Custom Post Type Display in Archive and Category Pages

Posted in: Newsmag
Post count: 39

I have added the custom post type to td_data_source

I added this code:
//init the array
$wp_query_args = array(
'ignore_sticky_posts' => 1,
'post_type' => array('post','reviews')
);

Now it works in most places, but not in archive area. Is there another file that I am missing that needs to be edited.

these sections show up in search and everything else, just not in the archive or Category pages for some reason.

Any ideas?

Post count: 6600

Hi,

Please try this filter in your theme functions file:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if(is_category() || is_tag()) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('post','cpt'); // replace cpt to your custom post type
$query->set('post_type',$post_type);
return $query;
}
}

Hope this helps!

Thanks

Post count: 39

Okay, that code worked and got it so all posts where showing. Although implementing that code, seems to give an odd error when in a category or archive section. Once activated posts display, but the menu also seems to show a selection of the posts rather then the categories. See here:
http://content.screencast.com/users/bfrye27/folders/Default/media/deb1df40-cd82-42f9-a61f-e835c1ba9f6b/oddmenu.jpg

Here is what it looks like in any other area of the site that is not a category:
http://content.screencast.com/users/bfrye27/folders/Default/media/329761ba-f8c1-46d6-8335-78a77f3ef1b0/normalmenu.jpg
Let me know your thoughts, it is possible I am missing a needed step

  • This reply was modified 11 years by bfrye26.
  • This reply was modified 11 years by bfrye26.
Post count: 6600

Hi,

Please also add 'nav_menu_item' along your custom post type, like this: http://screencast.com/t/sQSMDdurzS4
This should solve your problem.

Thanks

Post count: 39

Okay, That is almost where we need it. It added back the regular menu, but it still seems to have all the other articles listed as well: http://screencast.com/t/FSXjyyrPUuw

So it is much better then the old one: http://content.screencast.com/users/bfrye27/folders/Default/media/deb1df40-cd82-42f9-a61f-e835c1ba9f6b/oddmenu.jpg

Any ideas what is missing? Maybe I am doing something wrong. Here is my code:
http://screencast.com/t/qHYyTBGVTt

Post count: 6600

Hi,

Please try adding this: http://screencast.com/t/uq0gYbRl ..add this line: && empty( $query->query_vars['suppress_filters'])
Let me know if you still get it like this.

Thanks

  • This reply was modified 11 years by Lucian.
Post count: 39

Okay, I tried that, did not seem to make any changes

Here is the code: http://screencast.com/t/ubdjqf1nW

and here is what the menu displays like now: http://screencast.com/t/2nkIsLbO

Is it possible there is something wrong with the way I have the code? I feel it should not be this hard.

Let me know your thoughts,

thanks again!

Post count: 6600

Hi,

Please also check this after adding brackets to category and tag conditions, like this: http://screencast.com/t/t3c8SzVsYVD
And also please let me know how did you added your custom post types as you might need to do additional customization to this.
You can find here more suggestions on how to achieve this, give them a try:
http://css-tricks.com/snippets/wordpress/make-archives-php-include-custom-post-types/
I also suggest you try to find a solution for this online or use a plugin for it.

Thanks

Post count: 39

Okay, that fixed it!

The custom post type was added by the reviews system we use. It makes a new category named reviews, adds new fields etc. Sadly with so many reviews using it, it would be a rather large task to move everything over and would also break all the permalinks that are present.

Thanks so much for all your help!

Post count: 39

It took me a little while to get everything working, So I thought I would ensure listed the final code incase anyone was running into the same issues.

The final code to ensure it displays everywhere is:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if((is_category() || is_tag() ||  is_author()) && 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','cpt'); // replace cpt to your custom post type
$query->set('post_type',$post_type);
return $query;
}
}
  • This reply was modified 11 years by bfrye26.
Viewing 10 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic.