【问题标题】:ACF Query exclude date between 2 datepickers group inside repeaterACF 查询排除中继器内 2 个日期选择器组之间的日期
【发布时间】:2021-04-15 15:00:27
【问题描述】:

这快把我逼疯了。我会解释一下情况。 我有一个名为“affittiestivi”的自定义帖子 使用 ACF,我创建了一个带有组的中继器,该组由 2 个日期选择器字段组成,以让客户标记房屋出租的时期(例如从 2021/04/10 到 2021/04/20(所有日期都是以“Ymd”格式存储,以便于查询数据库) 中继器名称是“prenotazioni” 组名称是“prenotazione” 2 个日期选择器字段分别称为“inizio”和“fine”

用户可以搜索入住和退房日期(2个简单的html日期输入) 然后我需要查询该时间段内可用的每个帖子,如果房子是从 2021/04/10 到 2021/04/20 预订的,我搜索 2021/04/11 和 2021/04/19 它不应该显示。如果我在 2021/04/21 和 2021/04/30 之间搜索,则应显示该帖子。 我正在尝试元查询,但无论如何,帖子都不会显示。

总结:
Repeatear 名称:“Prenotazioni”
组名(内部中继器):“Prenotazione”
Datepicker 子字段1 : "inizio"
日期选择器子字段 2 : "fine"

任何帮助将不胜感激,对不起我的英语
到目前为止我有什么

/* Template Name: Ricerca Affitti */
<?php get_header();
 
$stype = $_GET['type'];
$searched_arr = $_GET['startdate'];
$searched_leave = $_GET['endate'];
 
$args = array(
     'post_type' => 'affittiestivi',
          'posts_per_page' => -1,
    );
$immobili = new WP_Query( $args ); //This query i't just to acces subfields value,
 //i know it's not the best practice tho and i'm not even sure i need this at this point
?>
 
<?php if($immobili->have_posts()) : while ($immobili->have_posts()) : $immobili->the_post(); ?>
<?php if( have_rows('prenotazioni') ): ?>
<?php while( have_rows('prenotazioni') ): the_row(); 
$data = get_sub_field('prenotazione');
$booked_from = $data['inizio'];
$booked_to = $data['fine'];
//This is working fine, var_dumped everything and they return yy-mm-dd, same format as the user input
 
$s_args = array(
    'post_type' => 'affittiestivi',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'order' => 'DESC',
    'meta_query' => array(
       'relation' => 'AND',
            array(
                'key' => 'prenotazione_inizio', //Is this the right way??
                'compare' => '>=',
                'value' => $searched_arr
            ),
             array(
                'key' => 'prenotazione_fine',
                'compare' => '<=',
                'value' => $searched_leave
            ),
    
          )
    );
endwhile;
endif;
//end acf row loop
endwhile;
endif;
//end custom Loop
$result = new WP_Query( $s_args );
if($result->have_posts()) : while ($result->have_posts()) : $result->the_post(); ?>

 //This query is always empty

<h1><?php the_title();?></h1>
 
<?php endwhile;
endif;
wp_reset_postdata();
get_footer()?>

编辑 由于我的进度为 0,我试图查看问题是什么,如果查询本身或 acf 字段,所以我完全删除了“repeater”字段,为了测试,我切换到一个简单的“int”字段,称为“ test”,这个查询现在正在工作并返回带有“test”字段值> = 4的帖子

    'post_type' => 'affittiestivi',
    'post_status' => 'publish',
        'meta_query' => array(
        'relation' => 'AND', // includes ACF stuff
            array(
        'key' => 'test',
        'value' => '4',
        'compare' => '>=',
            ),
    )
);

所以现在我 100% 确定问题出在(仍然是)“中继器字段”,我在中继器内的组子字段的税务查询“键”上做错了。 我做错了什么?

【问题讨论】:

    标签: php wordpress datepicker advanced-custom-fields


    【解决方案1】:

    请检查以下代码。您可以使用以下查询来查询日期范围。

    $args = array(  
    'post_type' => 'events',
    'posts_per_page' => -1,
    'meta_key' => 'start_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'start_date',
            'value' => $today,
            'compare' => '<=',
            'type' => 'NUMERIC',
        ),
        array(
            'key' => 'end_date',
            'value' => $today,
            'compare' => '>=',
            'type' => 'NUMERIC',
        ),
    
    ));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多