Posted: February 14, 2022

Sometimes you need to modify a query without wanting to create a page template.
With Custom Post Types UI, you can create archives for your custom post types using their taxonomy. And it requires some PHP to edit the default sorting of these pages.
For example, let’s assume we have Musician as a custom post type. Then, we create a new Taxonomy called Style - and inside that taxonomy, style, we have Jazz, and R&B.
// For City Pages, Order Items by Menu Order
function sort_tax_by_order( $query ) {
if ( $query->is_tax( 'city' ) && $query->is_main_query() ) {
$query->set( 'orderby', 'menu_order' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'sort_tax_by_order' );
This is an old loop used to create a shortcode, which runs the same query as the Elementor custom filter "Elementor Custom Query - Artist Products"
<?php>
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'associated_artist', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
);
?>
We respect your data. Privacy Policy
Share your thoughts!