Skip to content

Commit

Permalink
Update event query render
Browse files Browse the repository at this point in the history
Use query method to pre-fetch post IDs
  • Loading branch information
misfist committed Mar 25, 2022
1 parent b4a106d commit a85de5b
Showing 1 changed file with 19 additions and 65 deletions.
84 changes: 19 additions & 65 deletions src/App/Blocks/src/eventQuery/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WpActionNetworkEvents\Common\Util\TemplateLoader;
use WpActionNetworkEvents\App\Blocks\Blocks;
use WpActionNetworkEvents\App\Admin\Options;
use WpActionNetworkEvents\App\General\Queries;

/**
* Renders the `wp-action-network-events/event-query` block on the server.
Expand All @@ -27,6 +28,7 @@ function render( $attributes, $content, $block ) {
$default_display = $block_type_attributes['display']['default'];
$event_options = \get_option( Options::OPTIONS_NAME );


$defaults = array_merge(
array(
'post_type' => Event::POST_TYPE['id'],
Expand Down Expand Up @@ -54,17 +56,24 @@ function render( $attributes, $content, $block ) {
)
);

$args = wp_parse_args( $args, $defaults );
$args = \wp_parse_args( $args, $defaults );

$events = Queries::getAnEventIds( $args['scope'] );

$taxonomy = $args['taxonomy'];

if ( 'start' === $args['orderby'] ) {
$args['orderby'] = 'meta_value';
$args['meta_key'] = '_start_date';
}
$query_args = array(
'post_type' => array( Event::POST_TYPE['id'] ),
'posts_per_page' => $args['posts_per_page'],
'orderby' => 'meta_value',
'order' => $args['order'],
'meta_key' => 'start_date',
'meta_type' => 'DATETIME',
'post__in' => $events,
);

if ( ! empty( $args['event-tags'] ) ) {
$args['tax_query'] = array(
$query_args['tax_query'] = array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
Expand All @@ -73,67 +82,12 @@ function render( $attributes, $content, $block ) {
);
}

$args['meta_query'] = array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'is_hidden',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'is_hidden',
'value' => '1',
'compare' => '!=',
),
array(
'key' => 'is_hidden',
'value' => true,
'compare' => '!=',
),
),
array(
'relation' => 'OR',
array(
'key' => 'hidden',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'hidden',
'value' => '1',
'compare' => '!=',
),
array(
'key' => 'hidden',
'value' => true,
'compare' => '!=',
),
),
array(
'key' => 'visibility',
'value' => 'private',
'compare' => '!=',
),
);

if ( isset( $event_options['hide_canceled'] ) && 'checked' == $event_options['hide_canceled'] ) {
$args['post_status'] = array( 'publish' );
}
// echo '<pre>';
// var_dump( $args['scope'], $query_args );
// echo '</pre>';

if ( isset( $args['scope'] ) && 'all' !== $args['scope'] ) {
$compare = '>=';
if ( 'past' === $args['scope'] ) {
$compare = '<';
}
$args['meta_query'][] = array(
'key' => 'start_date',
'value' => \date( 'c' ),
'compare' => $compare,
'type' => 'DATETIME',
);
}

$query = new \WP_Query( $args );
$query = new \WP_Query( $query_args );
$output = '';

if ( $query->have_posts() ) :
Expand Down

0 comments on commit a85de5b

Please sign in to comment.