How to Make a Custom Query in Elementor Posts

How to Make a Custom Query in Elementor Posts

Creating custom queries in Elementor can significantly enhance the functionality of your website by allowing you to display specific posts based on custom criteria. In this article, we’ll walk you through the process of creating a custom query to display posts with titles containing the word “Brown”. 

function custom_query_for_brown_post($query) {
    // Avoid duplicate queries
    static $already_ran = false;
    if ($already_ran) {
        return; // If the query has already run, do not run it again
    }
    $already_ran = true;
    // Prepare an array for post IDs that should be included in the query
    $matching_post_ids = array();
    // Get all posts of type 'post'
    $all_posts = get_posts(array(
        'post_type' => 'post',
        'posts_per_page' => -1, // Get all posts without limits
        'fields' => 'ids', // Get only post IDs to save memory
    ));
    // Check if posts are found
    if (empty($all_posts)) {
        error_log('No posts of type post found.');
        return;
    }
    // Iterate through all posts
    foreach ($all_posts as $post_id) {
        // Get the post title
        $post_title = get_the_title($post_id);
        // Check if the title contains the word "Brown"
        if (strpos($post_title, 'Brown') !== false) {
            // If the word is found, add the post ID to the array
            $matching_post_ids[] = $post_id;
            error_log('Found post with ID: ' . $post_id . ', title contains the word Brown.');
        }
    }
    // If there are matches, apply them to the query
    if (!empty($matching_post_ids)) {
        $query->set('post__in', $matching_post_ids);
        error_log('Posts to display: ' . implode(', ', $matching_post_ids));
    } else {
        // If no matches, do not display posts
        $query->set('post__in', array(0));
        error_log('No matches found, no posts to display.');
    }
}
add_action('elementor/query/brown_query', 'custom_query_for_brown_post');

Explanation

  1. Avoid Duplicate Queries: The function uses a static variable $already_ran to ensure the query runs only once.
  2. Prepare Post IDs Array: An array $matching_post_ids is prepared to store IDs of posts that match the criteria.
  3. Get All Posts: The get_posts function retrieves all posts of type ‘post’ without any limit on the number of posts.
  4. Check for Posts: If no posts are found, an error is logged, and the function exits.
  5. Iterate Through Posts: Each post is checked to see if its title contains the word “Brown”. If it does, the post ID is added to the array.
  6. Apply Matches to Query: If matching posts are found, their IDs are set in the query. If no matches are found, the query is set to return no posts.

How to Use

  1. Add the Code: Add the above code to your theme’s functions.php file or use a code snippets plugin.
  2. Set Query ID in Elementor: In the Elementor editor, set the query ID to brown_query in the relevant widget’s query settings.

By following these steps, you can create a custom query in Elementor to display posts with specific criteria, enhancing the customization and functionality of your website.

© 2024 Myroslav Tiukhtii