Using custom post types in tag archive pages
-
Hi 🙂
My site has a blog and a travel guide, so I have created a custom post type called ‘entries’ to keep the guide entries separate from the blog posts. Both post types are using the same core WP categories and tags. On the tag page, however, standard WP coding only brings up the posts. I would like to load both the entries and the posts but not all mixed together. I would like to have all the entries listed first, with a heading saying Guide Entries, with the blog posts underneath following a Blog Posts heading.
I have managed to include the entries when post_type is called using the following code in functions.php:
function add_custom_types_to_tag_archives( $query ) {
if( is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {
$post_types = array( ‘post’, ‘entries’ );
$query->set( ‘post_type’, $post_types );
return $query;
}
}
add_filter( ‘pre_get_posts’, ‘add_custom_types_to_tag_archives’ );And then have managed to use the code below to get the tag.php to load only the entries.
<?php $query = new WP_Query( ‘post_type=entries’ ); ?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php x_get_view( $stack, ‘content’, get_post_format() ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata();?>That is all working fine, and I feel like I’m really close to getting it to work, but am left with 3 questions.
#1. How can I repeat that second piece of code to only return the posts for the second part of the process? I tried just pasting it below the first code and changing the post_type to post, but that just broke the site (I’m new to PHP so don’t know how to do things and am basically copying and pasting from other support forums).
#2. What do I need to include in the tag page to have the titles come up between the two sets of results?
And #3. How can I limit the first piece of code to only apply to processes in the content area? I have widgets in the footer that also use post_type and they are also being affected. I’m thinking some kind of if statement relating to the div class maybe, as that is how the theme loads the page.
Thanks for your help!!
The topic ‘Using custom post types in tag archive pages’ is closed to new replies.