• Resolved islp

    (@islp)


    In my plugin, I registered a Custom Post Type with its own archive (has_archive = true).

    The archive displays a list of posts in a sort of grid (by default, the theme provides this grid, used by search results too).

    Any single item in the list displays the direct link to the related post AND its categories (any category features a clickable link).

    What happens: when I click one of the categories created within the admin area of the custom post type, no post is found, even if there are many.

    This is a typical link:
    https://www.my.domain/category/mycategory/mysubcategory/

    This happens with this scheme too:
    https://www.my.domain/category/mycategory/

    At the same time, categories created from inside the traditional Post admin area suffer no such issue.

    Why are my categories ignored?

    [UPDATE]

    After some research, I found the only way to get (not entirely…) what I want is use this code in functions.php:

    add_action('pre_get_posts', function($query) {
    if ( ! is_admin() && $query->is_main_query() ) {
    
        if ( is_archive() || is_category() ) {
            $query->set( 'post_type', 'myCustomPost' );
        }
    
    }
    });

    Is this safe or wrong?

    I found this thing too: if I have a nested category, this thing breaks my navigation.

    • This topic was modified 3 years, 8 months ago by islp.
    • This topic was modified 3 years, 8 months ago by islp.
    • This topic was modified 3 years, 8 months ago by islp.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    If it’s what you want and doesn’t break anything it’s fine. But you’re preventing the archive or category listing of any regular “post” post type.

    Use of is_main_query() normally prevents any interference with navigation item queries. You apparently aren’t using default wp_nav_menu() for navigation. Your code may need another conditional to exclude navigation related queries.

    Thread Starter islp

    (@islp)

    @bcworkz : first of all, thanks for your kind reply.

    The website theme (a commercial one) has no archive at all (there isn’t an archive.php, neither).

    The problem I’m trying to solve is the following: the users of this website are used to a certain UX, so an item from the search list is a rectangle with a title and some categories. When they click one category, they WANT to get any post from that category. Now, if they click an identical item in the CustomPosts archive, they want the same behavior. I could also create a custom archive-CustomPost.php and get rid of the categories, but I feel I would loose a useful functionality.

    For navigation: I’m terribly sorry, what I called navigation is a sort of “breadcrumbs” in the theme that indicates where are you in the website, eg. Home > CustomPost > … (this thing breaks if I add subcategories)

    Thread Starter islp

    (@islp)

    I found this: https://wpshout.com/practical-uses-pre_get_posts/ but I could not understand why he checks empty($query->query_vars['suppress_filters']) too, I’m investigating ??

    Moderator bcworkz

    (@bcworkz)

    WP has archives whether they’re used or not ?? If there is no archive.php, WP will use index.php. If the request is not singular, it’s an archive of some sort.

    Breadcrumb trails ought to support hierarchical categories. If not, I’d refer you back to the devs responsible.

    If suppress_filters were not empty, it’s an indication the query shouldn’t be altered through filtering, so it’s logical that altering through pre_get_posts shouldn’t be done either. It would be abnormal for a main query to also suppress filters. Checking if filters are suppressed is maybe similar (but different) to checking if it’s the main query.

    I think you’re saying that if one follows a category link from a posts listing, we want only posts and not CPTs in that category. Conversely, if one follows a category link from a CPT listing, we want only CPTs and not posts in that category. The best way to do that is to include a "post_type=$current_type" sort of query var in all category links. Alternatives to passing the type via URL is through cookies or session variables. The query var option would not require any alteration through pre_get_posts. Only the template code outputting the link would need alteration.

    There is one other alternative — list both post types in the category under both or all situations ??

    Thread Starter islp

    (@islp)

    Hi @bcworkz, and thanks for taking the time to reply.

    I found this works for me, that is, in the context of my theme:

    add_action('pre_get_posts', function($query) {
    
       if ( ! is_admin() && $query->is_main_query() && empty( $query->query_vars['suppress_filters'] )) {
    				
          if(is_archive()){
    
              if(is_category()){
    		$query->set( 'post_type', ['post', 'myCustomType'] );
    			}else{
    		$query->set( 'post_type', 'myCustomType' );
    	  }
    
          }			
    }});

    I don’t know if this has some contraindication, but at the moment anything works fine: I must thank the Query Monitor plugin developer because sometimes it’s really not easy to understand what it is happening, what templates are involved, etc.

    Breadcrumb trails are developed in a strange way inside the core directory of the theme: the theme clearly doesn’t support the visualization of nested categories inside the categories archive (it supports the current category and it displays its name: nested categories break the visualization: I must say, I verified this happened even before I created the custom post type, with regular posts).

    ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom post type categories ignored’ is closed to new replies.