Skip to content

Instantly share code, notes, and snippets.

@mi-ca
Last active August 18, 2020 16:29
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 mi-ca/b02ea6d9990c4bb8fc5f474ed7b341ea to your computer and use it in GitHub Desktop.
Save mi-ca/b02ea6d9990c4bb8fc5f474ed7b341ea to your computer and use it in GitHub Desktop.
WP_Query args with repeater dates
<?php
add_action( 'pre_get_posts', 'custom_query_vars' );
function custom_query_vars( $query ) {
if ( !is_admin() && $query->is_main_query()) {
if ( is_post_type_archive('evenements')) {
$today = date('Ymd');
$sixMonthsPast = date('Ymd',strtotime('today -6 months'));
$sixMonths = date('Ymd',strtotime('today +6 months'));
$meta_query=array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'dates_$_date_debut',
'compare' => 'BETWEEN',
'value' => array($sixMonthsPast,$sixMonths),
),
array(
'key' => 'dates_$_date_fin',
'compare' => 'BETWEEN',
'value' => array($today,$sixMonths),
)
),
);
$query->set( 'posts_per_page', -1 );
$query->set( 'meta_query', $meta_query );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
}
}
return $query;
}
<?php
$meta_query = array('relation' => 'OR');
// construction de la requête car wpquery n'accèpte pas les tableaux
foreach ($taxonomies as $key=>$v) {
$tlist[]=$key;
$meta_query[] = array(
'key' => 'thematiques',
'value' => '"' . $key . '"',
'compare' => 'LIKE'
);
}
$args=array(
'post_type' => 'evenements'
,'meta_key' => 'dates_$_date_fin'
,'order_by' => 'meta_value_num'
,'order' => 'ASC'
,'post__not_in' => array($id)
,'posts_per_page'=> 3
,'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'dates_$_date_fin',
'compare' => '>=',
'value' => $today,
),
$meta_query
)
);
$the_query = new WP_Query( $args );
/*
*
*
*
*/
function getEventsHome($atts){
$today = date('Ymd');
$atts = shortcode_atts(
array(
'num' => '10',
'ordre' => 'DESC',
'exclude' => null
),
$atts);
// trop compliqué de prendre en considération l'heure de fin dans la requête
$args=array(
'post_type' => 'evenements'
,'order_by' => 'meta_value_num'
,'order' => 'ASC'
,'posts_per_page'=> $atts['num']
,'meta_key' => 'dates_$_date_fin'
,'meta_value'=>$today
,'meta_compare'=>'>='
// )
// ,'meta_query' => array(
// 'key' => 'dates_$_date_fin',
// 'compare' => '>=',
// 'value' => $today
// )
);
if(isset($atts['exclude']) && !empty($atts['exclude'])){
$args['post__not_in']=explode($atts['exclude']);
}
$the_query = new WP_Query( $args );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment