Update
I’ve found the solution, by first filtering with is_main_query()
, and then using the is_post_type_archive( 'tribe_events' )
conditional statement as was proposed in the first answer.
function order_calendar_by_artist( $query ){
if ( is_admin() || !$query->is_main_query() ) {
return;
}
if( is_post_type_archive( 'tribe_events' ) ){
$query->set( 'meta_key', 'artist' );
$query->set( 'orderby', 'meta_value' );
}
}
add_filter( 'pre_get_posts', 'order_calendar_by_artist', 99 );
Old Answer 2
Try the following (found on the plugin forum):
function order_calendar_by_artist( $query ) {
if ( !empty( $query->tribe_is_multi_posttype ) ) {
$query->set( 'meta_key', 'artist' );
$query->set( 'orderby', 'meta_value' );
}
}
add_action( 'pre_get_posts', 'order_calendar_by_artist' );
It appears you need to find the correct $query
for the calendar plugin. You could always print_r( $query);
to see which queries are running on that page and adjust the conditional accordingly. Might take a while but it’s all I can think of at the mo.
Old answer 1
You could wrap the function code inside a conditional statement like so:
function order_calendar_by_artist( $query ) {
if ( is_post_type_archive( 'tribe_events' ) ) {
$query->set( 'meta_key', 'artist' );
$query->set( 'orderby', 'meta_value' );
}
return $query;
}
add_filter( 'pre_get_posts', 'order_calendar_by_artist', 99 );
What that should do is only adjust the query on the tribe_events
post type archive page. The conditional may need adjusting dependent on where the query needs to take affect. At the moment you’re using the pre_get_posts
action which is affecting every single query on the site so you’ll need to hone into a specific area in which you want your function to run (i.e. using a conditional).
You can also use the following to avoid affecting the admin side
if ( is_post_type_archive( 'tribe_events' ) && !is_admin() ) {
...
}
I’ve not actually tested this however it should work… (he says)
Failing that you might need to utilise the WP_Query()
function and create a new query wherever you need to re-order your posts.
$args = array(
'post_type' => 'tribe_events',
'orderby' => 'meta_value_num',
'order' => 'ASC', //or DESC
'meta_key' => 'artist'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
//Loop stuff
endwhile;
endif;
wp_reset_postdata();