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.)
archive.php
etc. Also functions such asis_post_type_archive
etc in apre_get_posts
should always reference the$query
object, aka don't dois_single()
instead do$query->is_single()
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?