我写Elementor失败了$meta_query
职能so职位将由meta_value_num
给定密钥的。
每个帖子都有一个键的数值google_unique_page_views
我的示例如下:
// Custom query to order \'recommended reading\' posts by populatrity
add_action( \'elementor/query/my_custom_filter\', function( $query ) {
$meta_query = $query->get( \'meta_query\' );
if ( ! $meta_query ) {
$meta_query = [];
}
$meta_query[] = [
\'order\'=> \'DESC\',
\'key\' => \'google_unique_page_views\',
\'orderby\' => \'meta_value_num\',
];
$query->set( \'meta_query\', $meta_query );
});
我需要包括
value
查询
最合适的回答,由SO网友:Buttered_Toast 整理而成
假设您希望使用该元键获取帖子,而不考虑内容,并按该元值排序,则需要设置两个属性。
因此,基于您的问题的代码如下
// Custom query to order \'recommended reading\' posts by populatrity
add_action(\'elementor/query/my_custom_filter\', function ($query) {
if (empty($meta_query = $query->get(\'meta_query\'))) $meta_query = [];
// add our condition to the meta_query
$meta_query[\'google_unique_page_views\'] = [
\'key\' => \'google_unique_page_views\',
\'compare\' => \'EXISTS\',
];
// set the new meta_query
$query->set(\'meta_query\', $meta_query);
// set the new orderby
$query->set(\'orderby\', [
\'google_unique_page_views\' => \'DESC\' // or ASC, based on your needs
]);
});