WP_Query complex

0

I'm putting together a calendar of events in Wordpress and I need to list all the events of the week grouped by the current day. I have events that last only one day and others that last the whole month. I created the custom post type "Events" and the custom fields "data_inicio" and "data_fim". How would I get the query that returns the custom post types "events" for a day but also the ones that are in progress?

    
asked by anonymous 19.04.2017 / 18:26

2 answers

1

Does 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 published events if used inside an archive-events.php which is basically an archive.php but exlusive of your post type events or a page.php whatever might be page-events.php

link

link

I'm not an expert on WordPress, but I think it would be something like that.

    
19.04.2017 / 19:12
1

So I understand you could do a query searching for all future and happening events.

/**
* 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 in only the week's events 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'
    )
),
    
21.02.2018 / 20:06