Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to figure out the best way to solve this problem. I have a couple of archive type pages that display content from multiple post types at once. For example, I've overridden the default search behavior of WordPress with my own search functionality. Since the core WordPress search isn't used, the global $wp_query variable is empty and the standard loop functions (have_posts(), the_post(), etc.) won't work.

My search class produces results that are ranked by relevance and grouped by post type. The results look like this:

$results = array(

    'page'  => array(
        'post_id' => {WP_Post object},
        'post_id' => {WP_Post object},
        'post_id' => {WP_Post object}
    ),
    'post'  => array(
        'post_id' => {WP_Post object},
        'post_id' => {WP_Post object}
    ),
    'custom_post_type' => array(
        'post_id' => {WP_Post object},
        'post_id' => {WP_Post object}
    )
);

What I'm doing now to output these is pretty hideous. I'm looping through each post type and outputting a <section> for it. Then, in the template for the post type sections I'm running the following loop to output the individual posts:

foreach ( $my_posts as $my_post ) {

    $GLOBALS['post'] = $my_post;
    setup_postdata( $my_post );

    get_template_part( 'content-multi', $post_type );
}

The issue is that I want the template parts to be useable in standard WordPress loops and in situations (like the search page) where I'm overriding the standard WordPress query. Hence, the hacks with the global variables and the setup_postdata(). Can anyone recommend a better way to accomplish this goal?

UPDATE: So, it seems like the way I'm constructing the custom loop isn't that bad after all. I'm still trying to figure out the feeds for these pages though. RSS is pretty new to me. Can anyone give me some pointers on generating these feeds?

share|improve this question
2  
I don't really see anything wrong with what you're doing; globalizing $post and using setup_postdata() are very standard, well-established techniques for dealing with posts that don't come straight out of the standard loop; they're not usually considered hacks. –  ebohlman May 12 '13 at 11:40
    
try to use foreach ( $my_posts as $post ) { setup_postdata($post); ... } since most of the template functions will search for a $post global variable to get the post_id from. –  Dan Ștefancu May 12 '13 at 12:26
    
That's interesting. I didn't realize that was a standard practice. The other problem I see is RSS feeds. They will be empty since they are generated from the main query. Will I have to write my own RSS template as well? –  Dominic P May 12 '13 at 18:44
    
@ebohlman, I've been going through my unanswered questions and doing a little clean up. If you want to post your comment as an answer, I'll mark it as accepted. It was the info I needed. –  Dominic P Sep 19 '13 at 18:51
add comment

1 Answer

up vote 1 down vote accepted

I don't really see anything wrong with what you're doing; globalizing $post and using setup_postdata() are very standard, well-established techniques for dealing with posts that don't come straight out of the standard loop; they're not usually considered hacks.

share|improve this answer
    
wp_reset_postdata() should also be called after the custom loop end. –  G. M. Sep 21 '13 at 2:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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