I am currently working on two sites that make extensive use of custom post types. For both we have a defined number of items (cpts) that fall under a particular bucket (categories).
One of the sites is for a design shop, so the cpts are websites, logos, business cards, etc. And the categories are all the clients (several hundred).
Since the number of categories is quite large and always changing, I need to use a category.php template to display the collected projects (cpts) for a particular client.
I also need my cpts to display in a certain order--so websites in the top section of the page, logos in the bottom section, for example.
I initially created mulitple loops using query_posts & wp_reset_query. This got the posts to show where I wanted, but pagination was screwed up and after reading more I realized using query_posts is a big no-no.
However, whenever I use WP_Query it seems to completely ingnore my post_type argument (and only post_type). For example:
<?php
$current_cat = get_query_var('cat');
$args1 = array(
'post_type' => 'event',
'cat' => $current_cat,
'offset' => 1
);
$query1 = new WP_Query( $args1 );
while ($query1->have_posts()) {
$query1->the_post();
echo '<li><a href="'.get_permalink( $post->ID).'">' . get_the_title() . '</a></li>';
} //endwhile;
?>
The other thing is that I am only seeing this issue on the category template. When I use a loop similar to the above on a page template it returns the correct post types. What am I missing here?
I am currently working on two sites that make extensive use of custom post types. For both we have a defined number of items (cpts) that fall under a particular bucket (categories).
One of the sites is for a design shop, so the cpts are websites, logos, business cards, etc. And the categories are all the clients (several hundred).
Since the number of categories is quite large and always changing, I need to use a category.php template to display the collected projects (cpts) for a particular client.
I also need my cpts to display in a certain order--so websites in the top section of the page, logos in the bottom section, for example.
I initially created mulitple loops using query_posts & wp_reset_query. This got the posts to show where I wanted, but pagination was screwed up and after reading more I realized using query_posts is a big no-no.
However, whenever I use WP_Query it seems to completely ingnore my post_type argument (and only post_type). For example:
<?php
$current_cat = get_query_var('cat');
$args1 = array(
'post_type' => 'event',
'cat' => $current_cat,
'offset' => 1
);
$query1 = new WP_Query( $args1 );
while ($query1->have_posts()) {
$query1->the_post();
echo '<li><a href="'.get_permalink( $post->ID).'">' . get_the_title() . '</a></li>';
} //endwhile;
?>
The other thing is that I am only seeing this issue on the category template. When I use a loop similar to the above on a page template it returns the correct post types. What am I missing here?
Share Improve this question asked Feb 18, 2016 at 17:23 jkd540jkd540 751 gold badge1 silver badge4 bronze badges 5 |1 Answer
Reset to default 0Your issue is definitely caused by the lack of a pre_get_posts
filter. Try this in your theme/functions.php
:
// so event cpt is found on assigned cat archive page
function jdk540_event_query_post_type($query) {
if ( ! is_admin() ) {
if( is_category() || is_tag() && $query->is_main_query() && empty( $query->query_vars['suppress_filters'] ) ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array( 'post', 'event', 'nav_menu_item');
$query->set('post_type', $post_type);
}
}
}
add_action('pre_get_posts', 'jdk540_event_query_post_type');
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744862581a4597790.html
'post_type'
and remove'cat'
and'offset'
? Do you get the right results? – N00b Commented Feb 18, 2016 at 17:27pre_get_posts
filter to change the main query rather than adding a second? You should never usequery_posts
– Tom J Nowell ♦ Commented Feb 18, 2016 at 17:47offset
is only there to return all minus the latest. I can remove that with no consequences. Just a vanilla main query on my category.php will return only posts of that category (as expected). But for some reason when I add arguments such as number or post type I now have to use$current_cat
or it returns all categories. Any idea why? – jkd540 Commented Feb 18, 2016 at 19:19