sort by custom field in taxonomy page

I want to sort posts by a custom field and applied following code.
It works on the archive pages, but it doesn’t work on the taxonomy pages.
The codes for archive and taxonomy are same, i.e., “archive-publications.php” and “taxonomy-publication_type.php” are identical.
Please help me.

function custom_arange_posts( $query ) {

// do not modify queries in the admin
if( is_admin() ) {
return $query;

// only modify queries for ‘event’ post type
if( isset($query->query_vars[‘post_type’])) {
if($query->query_vars[‘post_type’]==”publications”) {
$query->set(‘orderby’, ‘meta_value’);
$query->set(‘meta_key’, ‘year’);
$query->set(‘order’, ‘DESC’);
if($query->query_vars[‘post_type’]==”research”) {
$query->set(‘orderby’, ‘date’);
$query->set(‘order’, ‘ASC’);
if($query->query_vars[‘post_type’]==”people”) {
$query->set(‘orderby’, ‘title’);
$query->set(‘order’, ‘ASC’);

// return
return $query;


add_action(‘pre_get_posts’, ‘custom_arange_posts’);

Read more here:: sort by custom field in taxonomy page

Leave a Reply

Your email address will not be published. Required fields are marked *