I’ve an interesting challenge (can’t be a problem, it has to be possible to solve it).
I’m trying to sort an archive page on title, using an ACF taxonomy.
I tried it with the following code snippet:
add_action( 'pre_get_posts', 'new_sort_order');
function new_sort_order($query){
global $wp_query;
if (is_archive() && is_post_type_archive('card') ):
//Set the order ASC or DESC
$query->set( 'order', 'ASC' );
//Set the orderby
$query->set( 'orderby', 'title' );
endif;
};
‘card’ is also an ACF post type.
It does not work. When I remove the ‘is_post_type_archive’ it does, but then it sorts every archive page of every post type on title. That’s not the idea.
So I delved a bit deeper into the query, and I see the ‘is_post_type_archive’ function checks against the ‘post_type’ field of the global $wp_query, and the value of that field is ‘acf-taxonomy’.
So is there a way to retrieve the post_type value (‘cards’) from the query?
Thanks in advance
Wilko