If ‘platinum’ is also a value of the ‘listing_type’ custom field you can use this:
function my_post_queries( $query ) {
// not an admin page and is the main query
if ( !is_admin() && $query->is_main_query() ) {
// category and search pages
if ( is_category() || is_tag() || is_tax() || is_search() ) {
add_filter( 'posts_orderby', 'add_featured_orderby' );
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
function add_featured_orderby( $orderby ) {
global $wpdb;
$args = array(
'meta_key' => 'listing_type',
'fields' => 'ids',
'orderby' => 'rand',
'posts_per_page' => -1,
);
if ( is_category() || is_tag() || is_tax() ) {
$obj = get_queried_object();
$tax_query = array(
array(
'taxonomy' => $obj->taxonomy,
'field' => 'id',
'terms' => $obj->term_id,
)
);
$args['tax_query'] = $tax_query;
}
$platinum_args = $args + array( 'meta_value' => 'platinum' );
$featured_args = $args + array( 'meta_value' => 'featured' );
$platinum = (array) get_posts( $platinum_args );
$featured = (array) get_posts( $featured_args );
$posts = array_unique( array_merge( $platinum, $featured ) );
if ( $posts ) {
// add custom ordering
$sql = ' CASE';
$i = count( $posts );
foreach ( $posts as $post ) {
$sql .= " WHEN $wpdb->posts.ID = $post THEN $i";
$i--;
}
$sql .= ' ELSE 0 END DESC, ';
$orderby = $sql . $orderby;
}
return $orderby;
}
This will order the custom field posts randomly and the rest by post date.
This code will now also use this order on custom taxonomy and tag pages.
Just remove the taxonomies you don’t want in:
if ( is_category() || is_tag() || is_tax() || is_search() ) {
add_filter( 'posts_orderby', 'add_featured_orderby' );
}