Skip to content

Instantly share code, notes, and snippets.

@jo-snips
Forked from PaulHughes01/functions.php
Created May 7, 2013 22:10
Show Gist options
  • Save jo-snips/5536574 to your computer and use it in GitHub Desktop.
Save jo-snips/5536574 to your computer and use it in GitHub Desktop.
<?php
// Add this to your functions.php file to create a venue query that queries venues in order of number of events.
// You may need to add a limit statement if you want more events to be returned than the default for the query.
add_filter( 'pre_get_posts', 'my_function_to_get_venues_by_event_numbers', 10, 1 );
function my_function_to_get_venues_by_event_numbers( $query ) {
$post_type = (array) $query->query_vars['post_type'];
if ( !is_single() && $post_type == array( TribeEvents::VENUE_POST_TYPE) ) {
add_filter( 'posts_fields', 'my_posts_fields_for_venues_function', 20, 1 );
add_filter( 'posts_join', 'my_posts_join_for_venues_function', 20, 1 );
add_filter( 'posts_where', 'my_posts_where_for_venues_function', 20, 1 );
add_filter( 'posts_groupby', 'my_posts_group_by_for_venues_function', 20, 1 );
add_filter( 'posts_orderby', 'my_posts_order_by_for_venues_function', 20, 1 );
}
return $query;
}
function my_posts_fields_for_venues_function( $fields ) {
global $wpdb;
$fields = "{$wpdb->posts}.*, count(wp_postmeta.meta_value) as num_events";
return $fields;
}
function my_posts_where_for_venues_function( $where ) {
global $wpdb;
$where = " AND {$wpdb->posts}.post_type = '" . TribeEvents::VENUE_POST_TYPE ."' AND ({$wpdb->posts}.post_status = 'publish' OR {$wpdb->posts}.post_status = 'private') AND {$wpdb->postmeta}.meta_key = '_EventVenueID'";
return $where;
}
function my_posts_group_by_for_venues_function( $group ) {
global $wpdb;
$group = " {$wpdb->postmeta}.meta_value ";
return $group;
}
function my_posts_order_by_for_venues_function( $order ) {
$order = ' num_events DESC';
return $order;
}
function my_posts_join_for_venues_function( $join ) {
global $wpdb;
$join = " INNER JOIN wp_postmeta ON (wp_posts.ID = {$wpdb->postmeta}.meta_value)";
return $join;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment