0

I have a custom post type - vendors. They have a taxonomy vendor_category. I am attempting to sort the archive pages by the title (since it defaults to post date). Here is the code that I am putting into my functions.php file.

function sort_vendors_by_title( $query ) {
    // check if we're on the vendor_category archive page for the vendors post type
    if ( $query->is_main_query() && /*is_post_type_archive( 'vendors' ) &&*/ is_tax( 'vendor_category' ) ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'sort_vendors_by_title' );

That middle check - which I thought I would need (is_post_type_archive) always returns false. It doesn't make sense to me. I am on an archive page (vendor_categories) for a vendor type.

What am I not understanding here? (With it commented out, it does work. But, I like to be explicit in the types I'm selecting.)

3
  • when you say sort your archive pages, do you mean literal pages with page templates/page builders that show up under pages in the WP Admin screen? Or real archives that load archive templates such as archive.php etc. Also functions such as is_post_type_archive etc in a pre_get_posts should always reference the $query object, aka don't do is_single() instead do $query->is_single()
    – Tom J Nowell
    Commented Mar 25, 2023 at 19:19
  • @TomJNowell The URL is example.com/vendor-category/plumbers (as an example). So, I believe this is archive.php. I am applying a template over top of that to style it up, but that doesn't override the order. And, it's really just that is_post_type_archive is always returning false.
    – JasCav
    Commented Mar 25, 2023 at 19:36
  • do you have any custom WP_Query/get_posts/query_posts in that template you applied over the top? How did you apply the template? Is it a block template from the site editor?
    – Tom J Nowell
    Commented Mar 25, 2023 at 23:31

0

Browse other questions tagged or ask your own question.