0

I have two post types. One is post & other one is event. Both sharing same categories & tags. I am listing the posts by archive.php. Now I am listing the posts as general by two different templates like archive.php / archive-event.php. But now I want to list them in a single template with mixing the posts and order by date.

So any one would help me in this regard.

4
  • Use pre_get_posts to add post and event types to archive pages Jul 24, 2014 at 11:46
  • How to set two post types in that function. Could you please explain this? Jul 24, 2014 at 12:15
  • Do you need to display them in archive.php. Or in a page.php type template. Or in where? Jul 24, 2014 at 12:34
  • I need to list them in archive.php file. Jul 24, 2014 at 12:43

1 Answer 1

1

As I already mentioned in my comments, you must use pre_get_posts to add custom post types to archive pages.

One thing you must keep in mind here though, you have two archive pages here, archive.php and archive-event.php, so is_archive() is going to return true on both, which will alter both pages. You will need to exclude archive-event.php from your function. To do this, you'll need to make use of is_post_type_archive() to check and exclude that specific page

For all the possible parameters you can use with pre_get_posts, go have a look at WP_Query

function cpt_in_archive($query) {
  if ( !is_admin() && $query->is_main_query() && $query->is_archive() && $query->!is_post_type_archive( 'event' )) {

      $query->set( 'post_type', array( 'post', 'event' ) );
      $query->set( 'orderby', 'date' );

   }
}

add_action( 'pre_get_posts' , 'cpt_in_archive' );
0

Your Answer

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

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