Support » Themes and Templates » Custom post type tags/categories archive page

  • Hi.

    I’ve been playing with custom post types, and whilst initially I’m impressed with them, I have a few issues.

    I have converted some posts to custom type, and have have given them a category and some tags. This display fine and have hyperlinks. However, when I click the hyperlink, I get the archive page of the tag, but only posts from that tag (or category), not from my custom post type.

    What do I need to do?

Viewing 15 replies - 1 through 15 (of 39 total)
  • Put code below to your functions.php

    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 it will work for you.

    Khaled Hakim

    (@khaledhakimgmailcom)

    That is the same problem I was facing. I was using WordPress 3.0 built in widgets to add a category listing of categories I had created. The thing was that the categories I had created were for a Custom Post (content) Type that I had created myself using Custom Post Type UI plug-in that is so handy. The problem was that when I used the Category listing widget… although the listing of categories did show up… clicking on any single on of them took to me a 404 page on my own blog.

    I took the code provided by parandroid and pasted in my functions.php 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','<strong>cpt</strong><strong>'); // replace cpt to your custom post type
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    In the code above there is a variable “cpt”. I simply replaced this variable, as instructed by parandroid, with the name of the Custom Post Type I had created (which in my case was “Advertisements”) and voila… things worked perfectly.

    Thanks for sharing. I am just posting this comment as I noticed no one had posted anything that would point to the succcessful implementation of the above.

    If you have any questions or want any help doing any of the above mentioned, I’d be more than glad to help.

    Khaled Hakim

    This a great! Exactly what I needed. Now the issue I’m having is when I paste the filter in my functions.php, it conflicts with register_nav_menus function, basically it completely stripes out the code when viewing tag index pages. Very strange. Any ideas how to solve this? The search continues!

    if ( function_exists( 'register_nav_menus' ) ) {
      	register_nav_menus(
      		array(
      		  'foot_menu' => 'My Custom Footer Menu',
      		  'sidebar_menu' => 'Super Sidebar Menu'
      		)
      	);
    }

    The above is my register nav function, and I’m using the filter code from paradroids post. Thank you.

    @parandroid – I tried your code and it worked like a charm. Thanks for sharing!

    just want to add in for anyone who sees this, if you replace ‘cpt’ with a custom post type, then keep adding… like for the site I’m working on, ‘news’, ‘listings’) etc you can indefinitely draw that list out. just wanted to point it out for those of us working with (way too many?) multiple custom post types.

    bluekachina

    (@bluekachina)

    Same problem as “ecken” described.
    After pasting the code provided here, it resolved my problem (tag pages now display CPT records too), however, when viewing that resultant list of CPT records, the page that gets displayed is missing my WP3 menu.

    Has anybody else been able to resolve this problem?

    PS –> I pasted that code at the bottom of my functions.php file.

    I wanted to say this worked for me also, this is exactly what i was looking for, i did modify it and add is_post to the conditional since in my case i am displaying custom post types and posts within the same page, at least for now.

    I’m having the same problem that “ecken” described. When using this code, my custom menu in the primary navigation block disappears.

    Anyone have a workaround?

    Do you have a development site set-up? Since your dealing with a php file it may be a good idea to turn on debugging via your wp-config.php file.

    That may help trouble shoot.

    Thanks for the help.

    When WP E-Commerce plugin is activated I get alot of errors. When it is deactivated, not one error. It does not change the nav display.

    Any other suggestions?

    hmm, yeah mainly look for errors/notices coming from functions.php.

    try stepping thru the function with either die or echo, let me know the line right before it fails, again try this on a development site.

    I think I found the solution. I Googled “pre_get_posts register_nav_menus” and one of the results was this.

    So I added the nav_menu_item to the post_type array like this:

    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','articles','nav_menu_item');
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    This appears to fix the problem. The custom nav is now showing up on the tag and category pages.

    Is this the proper way to fix it or is there a better way?

    This most recent solution seems to have fixed the nav problem.

    Thanks!

    Even better: here’s a non-hacky fix for the nav-bar issue:
    Change this line:
    if(is_category() || is_tag()) {
    -to-
    if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

    Now you can take the nav_menu_item out of the post-type array.

    Hope that helps!
    Fix provided by Justin Tadlock: http://justintadlock.com/

    I have multiple custom post types (news,library,faqs). I show recent posts for all these cpt’s on all pages of site including category and tag pages.

    Using this function does what I want by adding a custom post type to basic category and tag pages/feeds.

    However It also breaks all my recent custom post type displays on these pages and forces all the recent posts to be from the custom post typed defined within the function.

    Any ideas on how to fix this so that this function only affects the main loop and not any other mini loops i have set up to display other cpt’s?

    thank you for any help you can provide.

Viewing 15 replies - 1 through 15 (of 39 total)
  • The topic ‘Custom post type tags/categories archive page’ is closed to new replies.