Skip to content

Instantly share code, notes, and snippets.

@zzramesses
Created October 27, 2017 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzramesses/aa4cea7bfc505e32b3a57364d6a4b5e4 to your computer and use it in GitHub Desktop.
Save zzramesses/aa4cea7bfc505e32b3a57364d6a4b5e4 to your computer and use it in GitHub Desktop.
<?php
use VP\VisitPlacer\Helpers;
class Event_Query {
public $query;
public function __construct() {
$this->query = $GLOBALS['wp_query'];
}
function get_query_ids() {
$category = get_query_var( 'event-category' );
$filter = get_query_var( 'event_filter' );
$location = get_query_var( 'location' );
$cost = get_query_var( 'cost' );
$start_date = get_query_var( 'start_date' );
$end_date = get_query_var( 'end_date' );
$args = array(
'post_type' => 'event',
'posts_per_page' => 100000,
'suppress_filters' => false,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'fields' => 'ids',
);
if( $start_date && $end_date ) {
$dates = array(
'event_start_after'=> $start_date,
'event_end_before' => $end_date,
);
$args = array_merge( $args, $dates );
}
if ( $category || $filter || $location || $cost ) {
$taxes = array(
'event-category' => esc_attr( $category ),
'event_filter' => Helpers\term_whitelist( 'event_filter', $filter ),
'location' => esc_attr( $location ),
'cost' => esc_attr( $cost ),
);
$tax_output = array();
foreach( $taxes as $key => $value ) {
if ( !empty( $value ) ) {
$tax_output[] = array(
'taxonomy' => $key,
'field' => 'slug',
'terms' => $value,
);
}
}
$tax_query = array(
'tax_query' => array(
'relation' => 'AND',
$tax_output,
)
);
$args = array_merge( $args, $tax_query );
}
$query = new \WP_Query( $args );
return $query->posts;
}
function count_results() {
return count( $this->get_query_ids() );
}
function get_term_ids( $taxonomy ) {
$post_ids = $this->get_query_ids();
$terms_array = array();
foreach( $post_ids as $post_id ) {
$term_ids = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'ids' ) );
foreach( $term_ids as $id ) {
$terms_array[] = $id;
}
}
return array_unique( $terms_array );
}
function count_taxonomy_posts( $taxonomy_term, $taxonomy ) {
$post_ids = $this->get_query_ids();
$args = array(
'post_type' => 'event',
'posts_per_page' => 1000,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $taxonomy_term->term_id,
),
),
'fields' => 'ids',
);
$tax_posts = get_posts( $args );
$intersect = array_intersect( $tax_posts, $post_ids );
return sprintf( '(%d)', count( $intersect ) );
}
function filtered_events() {
$post_ids = $this->get_query_ids();
foreach( $post_ids as $id ) :
$event = get_post( $id ); global $post; $post = $event; setup_postdata( $post );
get_template_part( 'partials/event', 'list' );
endforeach; wp_reset_postdata();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment