【问题标题】:Wordpress Elementor custom query with ACF带有 ACF 的 Wordpress Elementor 自定义查询
【发布时间】:2021-08-17 21:54:26
【问题描述】:

我不熟悉 PHP,但尝试在 theme/functions.php 中插入代码 sn-p。我的目的是根据事件自定义帖子类型和自定义字段查询一些帖子。

add_action( 'elementor/query/events', function( $query ) {
  $args = array(
            'post_type' => 'event',
            'meta_query' => array(
              array(
                  'key' => 'dstart',
                  'type' => 'DATE',
                  'value' => date('Y-m-d'),
                  'compare' => '>'            
              ),
            ),         
  );
$query->set($args);
} );

但它有这样的错误:

Uncaught ArgumentCountError: Too few arguments to function WP_Query::set(), 1 passed in wp-content/themes/hello-theme-child-master/functions.php on line 46 and exactly 2 expected in wp-includes/class-wp-query.php:1752
Stack trace:
#0 wp-content/themes/hello-theme-child-master/functions.php(46): WP_Query->set(Array)
#1 wp-includes/class-wp-hook.php(294): {closure}(Object(WP_Query))
#2 wp-includes/class-wp-hook.php(316): WP_Hook->apply_filters('', Array)
#3 wp-includes/plugin.php(484): WP_Hook->do_action(Array)
#4 wp-content/plugins/elementor-pro/modules/query-control/classes/elementor-post-query.php(373): do_action('elementor/query...', Object(WP_Query), Object(ElementorPro\Modules\Posts\Widgets\Posts))
#5 wp-includes/class-wp-hook.php(292): ElementorPro\Modules\QueryControl\Classes\Elementor_Post_Query->pre_get_posts_query_filter(Object(WP_Query))
#6 /var/www/

我能知道我误解了哪些部分吗?谢谢。

【问题讨论】:

    标签: php wordpress advanced-custom-fields elementor


    【解决方案1】:

    我想出了怎么做:

    add_action( 'elementor/query/event', function( $query ) {
        // Here we set the query to fetch posts with
        // post type of 'custom-post-type1' and 'custom-post-type2'
        $query->set( 'post_type', [ 'event' ] );
        
        $meta_query = $query->get( 'meta_query' );
    
        // 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[] = [
            'key' => 'dstart',
            'type' => 'DATE',
            'value' => date('Y-m-d'),
            'compare' => '>',
        ];
        $query->set( 'meta_query', $meta_query );
        
        
    } );
    

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 1970-01-01
      • 2021-06-09
      • 2023-03-20
      • 2013-07-28
      • 2012-04-27
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      相关资源
      最近更新 更多