0

I am trying to perform a merged query to get at most 6 posts from either the post post type and my events custom post type. The post query is based off of whether or not the Featured Image is set, and the events query is based off of whether or not the post has the meta value Featured selected. Everything works except: The query only returns 5 posts, and they are not balanced in the sense that [depending on which post I set as Featured/Add Featured Image too] they take precedence over one another.

For Example: If I add a Featured Image to a post post the results will be 3 event, 2 post. If I then check the Featured meta value in an events post type the results will be 4 event, 1 post.

Here is the code I am using to query and return posts:

        <?php
        $post_types = get_post_types('','names');
        $featuredEvents = get_posts(array(
            'post_type' => 'events',
            'post_status' => 'publish',
            'showposts' => 6,
            'meta_key' => '_ecmb_featured_event',
            'meta_value' => 'on'
        ));
        $featuredNews = get_posts(array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'showposts' => 6,
            'meta_key' => '_thumbnail_id'
        ));
        $mergedPosts = array_merge( $featuredEvents, $featuredNews );
        $postIDs = array();
            foreach( $mergedPosts as $item ) {
            $postIDs[] = $item->ID;
        }
        $uniquePosts = array_unique($postIDs);

        $posts = get_posts(array(
            'post__in' => $uniquePosts,
            'post_type' => $post_types,
            'post_status' => 'publish',
        ));

        foreach( $posts as $post ) :
        setup_postdata($post);
        if( get_post_type() == 'events' ) { ?>
                    This is a Featured Event
        <?php } elseif (get_post_type() == 'post') { ?>
                    This a post with the Featured Image set
        <?php } endforeach; wp_reset_query(); ?>

Can someone help me with this?

Edit:

Thanks to the answer below, I figured it out and used the following code:

$sliderQueryArgs = array(
    'posts_per_page' => 6,
    'post_type' => array(
        'post',
        'events'
    ),
    'orderby' => 'post_date',
    'meta_query' => array(
        'relation' => 'OR',
            array(
                'key' => '_ecmb_featured_event',
                'value' => 'on'),
            array(
                'key' => '_thumbnail_id',
                'compare' => 'EXISTS')
    )
);

$sliderPosts = get_posts( $sliderQueryArgs );
  • Can you describe the results you want from these queries? What do you mean by "balanced" and which query should take precedence, for example? – s_ha_dum Oct 22 '13 at 23:37
  • Good point — ideally it would be a 3/3 split, with either post type filling in when there aren't enough to meet the 3. On another note, it would be great if I could combine the two post types to display in order published, rather than all event posts first, then all post posts. But I won't push my luck! – Jody Heavener Oct 23 '13 at 0:02
0

You're missing the fact that get_posts() (or WP_Query) can take an array() of post types in a singular query. More info on Type Parameters in the Codex. The order will per default be the publishing date.

You can then specify it further by adding in stuff like your taxonomy_query, etc.

Keep in mind that you can orderby by meta_value as well as assigning multiple orderby arguments space separated:

'orderby' => 'meta_value post_date'

for example.

  • Thank you! Check out my edit above to see what I ended up doing. – Jody Heavener Oct 24 '13 at 20:53
  • @JodyHeavener Please add that as an answer and mark it as solution. If you don't feel comfortable with that, simply edit my answer and fill in the missing bits. Thanks. – kaiser Oct 24 '13 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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