在Elementor中两个ACF日期字段之间的查询

huangapple go评论59阅读模式
英文:

Query between two ACF Date Fields in Elementor

问题

我正在尝试查询一个事件,如果当前日期在由ACF设置的开始日期和结束日期之间(包括在内)。我的ACF字段的返回值格式也是Ymd。到目前为止我有这些...

add_action( 'elementor/query/now_playing', function( $query ) {
 	// 设置自定义文章类型
	$query->set( 'post_type', [ 'shows' ] ); 

	$meta_query = $query->get( 'meta_query' );

	// 如果在此过滤器运行时没有元查询,应将其初始化为空数组。
	if ( ! $meta_query ) {
		$meta_query = [];
	} 

	// 追加我们的元查询
	$meta_query[]=[
            'relation' => 'AND',
			array(
                'key' => 'start_date',
				'type' => 'DATE',
                'value' => date('Y-m-d'),
                'compare' => '<='
            ),
            array(
                'key' => 'end_date',
				'type' => 'DATE',
                'value' => date('Y-m-d'),
                'compare' => '>='
            ),       
        ];
 } );

目前,我没有得到任何结果,我可能做错了什么?

英文:

I am trying to query for an event if the current date is between (inclusive) the start date and end date set by ACF. The return value format for my ACF field is Ymd as well. Here's what I have so far...

add_action( &#39;elementor/query/now_playing&#39;, function( $query ) {
 	// Set the custom post type 
	$query-&gt;set( &#39;post_type&#39;, [ &#39;shows&#39; ] ); 

	$meta_query = $query-&gt;get( &#39;meta_query&#39; );

	// If there is no meta query when this filter runs, it should be initialized as an empty array.
	if ( ! $meta_query ) {
		$meta_query = [];
	} 

	// Append our meta query
	$meta_query[]=[
            &#39;relation&#39; =&gt; &#39;AND&#39;,
			array(
                &#39;key&#39; =&gt; &#39;start_date&#39;,
				&#39;type&#39; =&gt; &#39;DATE&#39;,
                &#39;value&#39; =&gt; date(&#39;Y-m-d&#39;),
                &#39;compare&#39; =&gt; &#39;&lt;=&#39;
            ),
            array(
                &#39;key&#39; =&gt; &#39;end_date&#39;,
				&#39;type&#39; =&gt; &#39;DATE&#39;,
                &#39;value&#39; =&gt; date(&#39;Y-m-d&#39;),
                &#39;compare&#39; =&gt; &#39;&gt;=&#39;
            ),       
        ];
 } );

Currently, I am not getting any results, what could I be doing wrong?

答案1

得分: 0

我通过将值的日期格式设置为current_time('Ymd')来使其工作。另外,看起来我在附加元查询之后忘记设置查询了。

add_action('elementor/query/now_playing', function($query) {
	$today = current_time('Ymd');
 	// 设置自定义文章类型
	$query->set('post_type', ['shows']);

	$meta_query = $query->get('meta_query');

	// 如果在此过滤器运行时没有元查询,它应该初始化为空数组。
	if (!$meta_query) {
		$meta_query = [];
	}

	// 附加我们的元查询
	$meta_query[] = [
		array (
		'key' => 'start_date',
		'value' => $today,
		'compare' => '<=',
		),
		array (
		'key' => 'end_date',
		'value' => $today,
		'compare' => '>=',
		)
	];

	$query->set('meta_query', $meta_query);
});
英文:

I was able to get this working by setting the date format of value to current_time(&#39;Ymd&#39;). Additionally, it looks like I had forgotten to set the query after appending the meta query.

add_action( &#39;elementor/query/now_playing&#39;, function( $query ) {
	$today = current_time(&#39;Ymd&#39;);
 	// Set the custom post type 
	$query-&gt;set( &#39;post_type&#39;, [ &#39;shows&#39; ] ); 

	$meta_query = $query-&gt;get( &#39;meta_query&#39; );

	// If there is no meta query when this filter runs, it should be initialized as an empty array.
	if ( ! $meta_query ) {
		$meta_query = [];
	} 

	// Append our meta query
	$meta_query[]=[
		array (
		&#39;key&#39; =&gt; &#39;start_date&#39;,
		&#39;value&#39; =&gt; $today,
		&#39;compare&#39; =&gt; &#39;&lt;=&#39;,
		),
		array (
		&#39;key&#39; =&gt; &#39;end_date&#39;,
		&#39;value&#39; =&gt; $today,
		&#39;compare&#39; =&gt; &#39;&gt;=&#39;,
		)
	];
	
	$query-&gt;set( &#39;meta_query&#39;, $meta_query );
 } );

huangapple
  • 本文由 发表于 2023年6月8日 01:34:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定