sort CPT on Custom Taxonomy Archive page

In my archive-product_category.php I have this (relevant) code

Are you sure that’s the correct name? Because for a post type archive, it should be archive-al_product.php and for a taxonomy archive page, it would be taxonomy-product_category.php.

Anyway, (as I said in the comments) if you want to target the taxonomy archive page, you should use is_tax(), or if it’s the post type archive page, then you would use is_post_type_archive().

  • Note that the first (and only) parameter for is_post_type_archive() is one or more post type names/slugs like al_product in your case. So in your code/attempts, that is_post_type_archive( 'product_category' ) is actually not correct.. =)

  • Also, your post type should have the has_archive argument set to true or a slug like al-products. That way, when you visit the archive at example.com/?post_type=al_product or example.com/al-products, is_post_type_archive() would return true.

So to correctly sort the posts (in the main WordPress query), you can try the following:

// In the rt_change_product_sort_order() function:
// Instead of using get_query_var(), you would use is_tax().
if ( is_tax( 'product_category' ) ) :
    // ... your code.
endif;

// Or use is_post_type_archive() to target the CPT archive.
if ( is_post_type_archive( 'al_product' ) ) :
    // ... your code.
endif;

// Or combine the conditional tags, if you want to:
if ( is_tax( 'product_category' ) || is_post_type_archive( 'al_product' ) ) :
    // ... your code.
endif;

Additional Resources