I’ve just started using Posts 2 Posts (brilliant plugin by the way) in combination with a custom post type called “Artist” and the WP Event Manager Plugin. My aim is to use P2P to link Artists to Events so an Event Page can display who is performing.
I have created a recurring number of events in Event Manager which understandably have incrementing post ID’s, e.g. 5,6,7. Let’s say for argument these events are all on subsequent Mondays. I then created a recurring event, say on a Friday which has post ID’s 8,9,10,11…..
Now I can order the order of the meta box in admin by post ID using something like:
function order_pages_by_ID( $args, $ctype, $post_id ) {
if ( 'posts_to_pages' == $ctype->name ) {
$args['orderby'] = 'ID';
$args['order'] = 'asc';
}
return $args;
}
add_filter( 'p2p_connectable_args', 'order_pages_by_ID', 10, 3 );
But this will now display all the Monday events and then all the Friday events when I’m trying to connect an Artist to an Event from the Artist’s page. What I’d like is to have Monday, Friday, Monday, Friday etc.
The event CPT has a custcom field called _event_start_date
which I can append to the label in the admin meta box by using:
function append_date_to_candidate_title( $title, $post, $ctype ) {
if ( 'my_connection_type' == $ctype->name && 'event' == $post->post_type ) {
$meta = get_post_meta( $post->ID, "_event_start_date" );
$title .= " (" . reset($meta) . ")";
}
return $title;
}
Is there any possible way to order the query for the admin meta box to take this custom field into account and order by this (or even order it in PHP afterwards)? I’ve seen a few examples of ordering the posts when using ‘get_connected()` from the frontend by nothing specifically related to the ordering in the admin meta box.
I finally found the answer. Turns out I just needed a better understanding of WPQuery.
It ended up being as simple as: