skip to Main Content

I am trying to combine two conditions and query the posts. But the output of the condition is not correct.

condition One :
check all posts in all categories , with meta_key slider .

condition Two :
query all posts with category id = 32378 .. no matter meta_key exist or not .

    // Query 1
            $args1 = array(
            'showposts' => theme_options('count_slider'),
            'meta_key'    => 'slider',
            'meta_value'  => true,
            );

    // Query 2
            $args2 = array(
              'showposts' => 5,
              'cat' => 32378,
            );

            // Combine the results
            $combined_args = array_merge($args1, $args2);

        $the_query = new WP_Query($combined_args);

i don’t know why this code shows just all posts with meta_key ‘slider’ (in just category 32378 ) .

I am trying to combine two conditions and query the posts. But the output of the condition is not correct.

I want a result like this :
all posts in category id 32378 , that . no matter meta_key exist or not + all posts in all categoris that have meta_key ‘slider’ with limitation of overal 7 post .

2

Answers


  1. $args1 = array(
        'showposts' => 5,
        'cat' => 32378,
    );
    
    $args2 = array(
        'showposts' => 7, 
        'meta_query' => array(
            array(
                'key' => 'slider',
                'value' => true,
                'compare' => 'EXISTS',
            ),
        ),
    );
    
    
    $combined_args = array(
        'relation' => 'OR', 
        $args1,
        $args2,
    );
    
    $the_query = new WP_Query($combined_args);
    
    Login or Signup to reply.
  2. The logic you are using by simply merging the two argument arrays with array_merge does not work as expected. In a WordPress query, you can’t just combine conditions in this way. What happens is that the second array overwrites the values of the first array instead of creating a logical OR condition.

    What you’re trying to achieve is a more complex query that cannot be done directly with a standard WP_Query. Instead, you need to use a custom SQL query or perform two separate queries and combine the results.

    Here is an example of how to perform two separate queries and combine the results:

    // Query 1: Posts in all categories with meta_key 'slider'
    $args1 = array(
        'showposts'   => theme_options('count_slider'),
        'meta_key'    => 'slider',
        'meta_value'  => true,
    );
    
    $query1 = new WP_Query($args1);
    $posts1 = $query1->posts;
    
    // Query 2: All posts in category 32378
    $args2 = array(
        'showposts' => 5,
        'cat'       => 32378,
    );
    
    $query2 = new WP_Query($args2);
    $posts2 = $query2->posts;
    
    // Combine the results
    $combined_posts = array_merge($posts1, $posts2);
    
    // Limit the results to 7 posts
    $combined_posts = array_slice($combined_posts, 0, 7);
    
    // Now you can iterate over $combined_posts and output your posts
    

    Note that this approach is not optimal, especially if you have many posts, as it puts more load on the database. Also, posts might appear twice if they meet both criteria. A solution would be to check the post IDs and remove duplicates.

    If performance is a concern, a custom SQL query would be the more efficient route, but that is more complex and requires a deep understanding of WordPress’s database structure.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search