Complex wp_query

Asked

Viewed 79 times

0

I am setting up a schedule of events in Wordpress and need to list all the events of the week grouped by current day. I have events that last only one day and others that last all month. I created the custom post type "Events" and the custom Fields "data_inicio" and "data_fim". How would the query that returns me the custom post types "events" of a day but also those that are in progress?

2 answers

1

Would a normal loop solve?

$eventos = array( 'post_type' => 'eventos', 'posts_per_page' => 10, 'meta_key' => 'data_inicio', 'orderby' => 'meta_value', 'order' => 'DESC' );
$loop = new WP_Query( $eventos );
  while ( $loop->have_posts() ) : $loop->the_post();
   the_title();
   the_content();
endwhile;

The above loop would show the published events if used within an Archive-events.php which is basically an Archive.php but exlusive of its post type events or any page.php can be page-events.php

https://developer.wordpress.org/themes/basics/template-hierarchy/#custom-post-types

https://wordpress.stackexchange.com/questions/30241/wp-query-order-results-by-meta-value

I’m not an expert on Wordpress but I think it would be something like this.

1

From what I understand you could do a query searching for all future events that are happening.

/**
* Agenda de eventos
*/
$today = date('Y-m-d');

$args = array(
    'post_type'      => 'post_talk', // seu post type
    'posts_per_page' => -1,
    'meta_key'       => 'data_inicio', // ordena os eventos por data de inicio
    'meta_type'      => 'DATE',
    'orderby'        => 'meta_value',
    'order'          => 'ASC',

    // busca todos os eventos que a data de termino seja maior ou igual a hoje
    'meta_query'     => array(
         array(
            'key'     => 'data_fim',
            'compare' => '>=',
            'value'   => $today,
            'type'    => 'DATE'
        )
    ),
);

$talks = new WP_Query( $args );

To bring only events of the week you can combine one more meta_query:

'meta_query' => array(
    'relation' => 'AND',
    array(
        'key'     => 'data_fim',
        'compare' => '>=',
        'value'   => $today,
        'type'    => 'DATE'
    ),
    array(
        'key'     => 'data_inicio',
        'compare' => '<=',
        'value'   => date('Y-m-d', strtotime('+1 week')), // +7 dias
        'type'    => 'DATE'
    )
),

Browser other questions tagged

You are not signed in. Login or sign up in order to post.