0

I have a custom post type that I want to have fully shown on the category page for that post type on my website as opposed to the regular blog section of the website. I have made a custom taxonomy and post type as shown below:

add_action('init', 'create_post_types');
function create_post_types() {
    register_taxonomy('tips-and-tricks-taxonomy', 'tat',
        array(  'hierarchical'      => true,
                'label'             => 'Destinations (Tips)',
                'singular_label'    => 'Destination (Tips)',
                'rewrite'           => array('slug' => 'tips-and-tricks', 'with_front' => false),
                'public'            => true,
                'show_ui'           => true,
                'show_tagcloud'     => true,
                '_builtin'          => true,
                'show_in_nav_menus' => true));

    register_post_type('tat', 
        array(  'labels'                => array(
                'name'                  => __('Tips and tricks'),
                'singular_name'         => __('Tip'),
                'add_new'               => __('Add new tip'),
                'edit_item'             => __('Edit tip'),
                'new_item'              => __('New tip'),
                'view_item'             => __('View tip'),
                'search_items'          => __('Search tips and tricks'),
                'not_found'             => __('No tips and tricks found'),
                'not_found_in_trash'    => __('No tips and tricks found in trash')),

                'has_archive'           => true,
                'public'                => true,
                'supports'              => array('title', 'editor', 'post-formats')
    ));
}

I then tried to alter the number of posts shown as follows:

function get_all_tat_posts($query) 
{
    if(!is_admin() && $query->is_main_query() && is_post_type_archive('tat')) {
        $query->set('posts_per_page', '-1');
    }
}
add_action('pre_get_posts', 'get_all_tat_posts');

When debugging, I get a $query->is_main_query() and is_post_type_archive('tat') both return false. I have no clue as to why I get this error, since all I do is navigate to the standard category page for that custom post type. I do not alter the query in any other way and I do not create the query myself.

Any ideas on what I might be doing wrong?

  • A taxonomy term page isn't a post type archive, it's a taxonomy archive. – Milo Aug 29 '17 at 17:22
  • Well, somehow that fixes the problem by changing it to is_tax, even though $query->is_main_query() keeps returning false. Any ideas on why that is? :) If you post this as an answer I will accept it. – Michiel Standaert Aug 29 '17 at 17:34
0

A taxonomy term page isn't a post type archive. Use is_tax() instead. Also, is_main_query() could be failing because it is not the main query. I know, obvious right. Without the rest of your php code, it is hard to tell. But the "main" query would be the query getting all the posts with that category. If this query is custom, or secondary, is_main_query() will fail.

  • Well, that is the problem. I just load the custom taxonomy page without altering the query anywhere else than shown above. So I know for a fact it is the main query. And yet it still returns false when I debug. – Michiel Standaert Aug 30 '17 at 13:51
  • If you view the contents of the global variables $wp_the_query and $wp_query on the page in question and compare them or post them here, that should give us or you some more insight. May even solve the problem. – Jeremy Muckel Aug 30 '17 at 14:32
  • @MichielStandaert hopefully you solved your issue! If not I can always help if you give more info. If my answer did help you solve your problem be sure to mark it as so. Have a good one! – Jeremy Muckel Aug 31 '17 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.