0

I'm trying to figure out how to order my posts by a custom field. I'm using a calendar (The events calendar), with a custom field called 'artist'. It works, though my query request affects all other post types as well which causes my menu and news items to disappear. Here's the code I have right now:

function order_calendar_by_artist( $query ){
   $query->set( 'meta_key', 'artist' );
   $query->set( 'orderby', 'meta_value' );  

   return $query;
}
add_filter( 'pre_get_posts', 'order_calendar_by_artist', 99 );

If I set the post_type as well, I also get a 404 error on my homepage:

function order_calendar_by_artist( $query ){
   $query->set( 'post_type', 'tribe_events' );
   $query->set( 'meta_key', 'artist' );
   $query->set( 'orderby', 'meta_value' );  

   return $query;
}
add_filter( 'pre_get_posts', 'order_calendar_by_artist', 99 );
0

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();
  • Thanks for your answer! I've tried is_post_type_archive, but it is not working unfortunately. I've also tried using get_post_type($post->ID) == 'tribe_events' but all these conditionals seem to get ignored. Since i'm trying to do this via my functions.php file, where do I put your last code snippet in my WP installation? – Tom Mar 4 '16 at 14:57
  • Hi Tom. You'd need to add this in the template in which the query is being performed. Is it part of a plugin or the theme? – Kieran McClung Mar 4 '16 at 15:01
  • It is part of "The Events Calendar" plugin – Tom Mar 4 '16 at 15:06
  • Updated answer although it's still not absolute I'm afraid. – Kieran McClung Mar 4 '16 at 15:26
  • I've updated the answer with the solution! Thanks for helping – Tom Mar 4 '16 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.