Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress-使用类型为日期的自定义字段在Elementor页面中显示帖子_Wordpress_Advanced Custom Fields_Elementor - Fatal编程技术网

Wordpress-使用类型为日期的自定义字段在Elementor页面中显示帖子

Wordpress-使用类型为日期的自定义字段在Elementor页面中显示帖子,wordpress,advanced-custom-fields,elementor,Wordpress,Advanced Custom Fields,Elementor,我试图实现这一点,同时尝试应用从这里和那里获取的一些代码 我在一个网站上使用Elementor页面生成器,我用ACF创建了一个自定义的帖子类型。在该帖子类型中,有一个用于开始日期的字段 其想法是,只有当日期等于或高于当前日期时,才会在主页中显示帖子 现在代码是这样的: add_action( 'elementor/query/por_data', function( $query ) { // Here we set the query to display posts // after spe

我试图实现这一点,同时尝试应用从这里和那里获取的一些代码

我在一个网站上使用Elementor页面生成器,我用ACF创建了一个自定义的帖子类型。在该帖子类型中,有一个用于开始日期的字段

其想法是,只有当日期等于或高于当前日期时,才会在主页中显示帖子

现在代码是这样的:

add_action( 'elementor/query/por_data', function( $query ) {
// Here we set the query to display posts
// after specific date
$query->set( 'date_query', array(
array(
        'after' => 'May 17, 2020', 
    )
) ); 
} );
这显然只是显示了2020年5月17日之后创建的帖子,这不是我想要的。其想法是从ACF字段中获取日期,将其与当前日期进行比较,并相应地显示结果。自定义字段类型名称为“curso”

找到了这段代码,但似乎可以将这两段代码合并在一起,因为我在编程方面的知识非常少(目前)


有人能帮忙吗?谢谢

我试着在评论中解释它-希望你现在能更好地理解它

add_action( 'elementor/query/por_data', function( $query ) {

    $meta_query = $query->get( 'meta_query' );
    // Append our meta query instead of overwriting all elementors own metaqueries
    if($meta_query == ""){
        $meta_query = array();
    }
    $meta_query[] = array(
        array(
            'key' => 'start_date', //Or whatever you field is called
            'value' => date('Y-m-d',time()), //Make sure that the format is correct here
            'compare' => '>' //Means that the value should be higher than the key, so this is what you want - If you also need the current dates posts, then you need to use '>=' instead
        )
    );
    $query->set( 'meta_query', $meta_query ); //since we appended our query, you can safely set the meta querie now. 
} );

您可能需要稍微调整它,但这将引导您正确的方向。

实际上可能还需要在数组之前使用
'relation'=>'和',
。不确定,谢谢你的意见。试图将代码添加到主题的function.php中,但继续显示所有帖子,似乎没有进行查询并将其与实际日期进行比较。格式为d-m-Y,下面是该字段的屏幕截图:
add_action( 'elementor/query/por_data', function( $query ) {

    $meta_query = $query->get( 'meta_query' );
    // Append our meta query instead of overwriting all elementors own metaqueries
    if($meta_query == ""){
        $meta_query = array();
    }
    $meta_query[] = array(
        array(
            'key' => 'start_date', //Or whatever you field is called
            'value' => date('Y-m-d',time()), //Make sure that the format is correct here
            'compare' => '>' //Means that the value should be higher than the key, so this is what you want - If you also need the current dates posts, then you need to use '>=' instead
        )
    );
    $query->set( 'meta_query', $meta_query ); //since we appended our query, you can safely set the meta querie now. 
} );