0

I need to display posts from multiple post type with two queries so I found this answer: How to display posts from multiple post type in random order in wordpress?

But can't use it to get post data like post content or post title, The author of the answer write I must set up by setup_postdata but I can't understand.

Any help, please?

2
  • What exactly are you trying to do? Do you really need two separate queries? Because with WP_Query and get_posts you can already query multiple post_types. In the query args you can add a array like: 'post_type' => array('post', 'product' ).
    – LWS-Mo
    Commented Oct 4, 2017 at 12:31
  • @LWS-Mo Yes i think i must use two separate queries because i want to set custom meta value and term for each one so i think i must use two separate queries, right? I'm waiting you i really need your help for this.
    – Trello
    Commented Oct 4, 2017 at 12:50

1 Answer 1

4

So you want to query posts from multiple post types, but also with different query-arguments like custom field, tax_query or categories.

As you did not post what you tried, have already or want to have, please try something like this:

We are using 3 queries to set everything up. You can add different arguments to each query.

Get post ID´s from our first post type, product:

$first_post_ids = get_posts( array(
    'fields'         => 'ids', // only return post ID´s
    'posts_per_page' => '5',
    'post_type'      => array('product'),
));

Get post ID´s from our second post type, post:

$second_post_ids = get_posts( array(
    'fields'         => 'ids', // only return post ID´s
    'posts_per_page' => '5',
    'post_type'      => array('post'),
));

Merge our two queries into one:

$merged_post_ids = array_merge( $first_post_ids, $second_post_ids);

Build the third query:

$wp_query = new WP_Query( array(
    'post_type' => 'any', // any post type
    'post__in'  => $merged_post_ids, // our merged queries
) );

The loop:

if ( $wp_query->have_posts() ) : 
    while ( $wp_query->have_posts() ) : $wp_query->the_post();

        //look at $post here !!
        //Example: $post->post_type;
        //this return the type of each post so you can do checks and stuff

        //for example show title and content
        the_title( '<h2>', '</h2>' );
        the_content();

    endwhile;

    // reset after query
    wp_reset_query();

else :
    echo 'Sorry, no posts matched your criteria.';
endif;

Update: Check if array_merge is not null to prevent any types showing

array_merge is used to merge 2 arrays, like the name suggests.

So for example, if $first_post_ids is a string, than it will not work. Thats why we use 'fields' => 'ids', in our queries.
Because as the Codex says:

'ids' - Return an array of post IDs.

And even if one of these two queries is empty (i.e. we have no posts), array_merge will still work.

But yes, we can check array_merge:

//... first two queries

$merged_post_ids = array_merge( $first_post_ids, $second_post_ids );

//check if the array_merge exists/not null
if ( $merged_post_ids ) {

    $wp_query = new WP_Query( array(
        'post_type' => 'any', // any post type
        'post__in'  => $merged_post_ids, // our merged queries
    ) );

    //... your query code here

}//END if $merged_post_ids

You can also expand it with an else statement to let another query run.

6
  • There's a small bug in your code, id no posts found in the post type your code will get any anther post type like pages and attachments so I try to change 'post_type' => 'any', to 'post_type' => array('product', 'post'), but it's not working. Any solution?
    – Trello
    Commented Oct 6, 2017 at 4:25
  • If we make something to check if array_merge is not empty this will solve the problem? if yes please update your answer.
    – Trello
    Commented Oct 6, 2017 at 8:08
  • @Trello yes, we can check array_merge. I just updated my answer with some details.
    – LWS-Mo
    Commented Oct 6, 2017 at 11:40
  • Thank you again you are a good person yea it's working with me but be known that to make it working we must check if array merge not empty from $wp_query = new WP_Query( array( to endif; because if we don't make this some pages also will appear as a post, I hope you understand me :)
    – Trello
    Commented Oct 6, 2017 at 11:59
  • @Trello sorry I dont fully understand what you mean :) We must first check $merged_post_ids. If it is not null, than we can use it in our third query. So our third query is inside the if check, because if array_merge is null we cannot use the third query.(So the whole $wp_query->have_posts() section is inside the if function) If you want to check before you merge the two arrays, you can maybe use (if) is_array.
    – LWS-Mo
    Commented Oct 6, 2017 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.