WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Type UI
Custom Post Type not Showing Catagory? (7 posts)

  1. WPDragon
    Member
    Posted 6 months ago #

    Hello..i created a Custom Post Type, and it works fine...however, I noticed when I select a 'Category' for the custom post - that it wont display any of the results for all the posts in this category together on the site.

    The "Category" works fine for all my normal posts, however it seems to only not work with my Custom Post Type.

    Any help would be greatly appreciated.

    thanks

    https://wordpress.org/plugins/custom-post-type-ui/

  2. Michael Beckwith
    Member
    Plugin Author

    Posted 6 months ago #

    You'll need to query for more than just the "posts" post type in your category archives. I highly recommend doing that with the pre_get_posts action hook.

    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

  3. WPDragon
    Member
    Posted 6 months ago #

    Hi @Michael Beckwith - thank you for the reply.

    The category.php page has a code like this below...so, if my Custom Post Type is called "Games" ...then would it be like this?

    query_posts(
          array(
            'ignore_sticky_posts' => 1,
            'posts_per_page' => $postsperpage,
            'paged' => $paged,
            'cat' => $cur_cat_id ) );
     <?php add_action( 'pre_get_posts', 'Games' ); ?>

    I'm fairly new to WordPress, so bare with me. :) thank you.

  4. Michael Beckwith
    Member
    Plugin Author

    Posted 6 months ago #

    you're mixing a lot of stuff up, with that attempt, but that's ok, you're learning.

    I'd recommend putting the following into your theme's functions.php file. It needs to go in that file.

    function add_post_type_to_category_archive( $query ) {
        if ( is_admin() || ! $query->is_main_query() )
            return;
    
        if ( is_category( 'MY CATEGORY SLUG' ) ) {
            $query->set( 'post_type', array( 'post', 'MY CUSTOM POST TYPE SLUG' ) );
        }
    }
    add_action( 'pre_get_posts', 'add_post_type_to_category_archive', 1 );

    Replace "MY CATEGORY SLUG" with the actual category slug you're trying to alter. Replace "MY CUSTOM POST TYPE SLUG" with the actual post type slug you registered.

    This snippet will set the post type to query to "post" and whatever your custom post type is ONLY IF you're on the category archive for the category that you'll specify.

  5. WPDragon
    Member
    Posted 6 months ago #

    It worked!..Thank you very much for your help with this!

    One lil' thing..i noticed it called the page "Archives" - most of the other Category pages have the Name of the Category as the header...is there a way to change the name, so it says "Games" - instead of "Archives" as the Title?

  6. Michael Beckwith
    Member
    Plugin Author

    Posted 6 months ago #

    That would be a change needed to be done to your theme files, which is outside the scope of our plugin.

  7. WPDragon
    Member
    Posted 6 months ago #

    ohh okay, Thanks again for your help..i appreciate it!

Reply

You must log in to post.

About this Plugin

About this Topic