Wordpress元素或带有ACF的自定义查询



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

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(ElementorProModulesPostsWidgetsPosts))
#5 wp-includes/class-wp-hook.php(292): ElementorProModulesQueryControlClassesElementor_Post_Query->pre_get_posts_query_filter(Object(WP_Query))
#6 /var/www/

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

我想好了怎么做:

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 );


} );

最新更新