Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1279175
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI'm failing at writing an Elementor $meta_query
function so posts will be ordered by meta_value_num
of a given key.
Each post has a numeric value for the key google_unique_page_views
My example is below:
// 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 );
});
Do I need to include the value
query?
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI'm failing at writing an Elementor $meta_query
function so posts will be ordered by meta_value_num
of a given key.
Each post has a numeric value for the key google_unique_page_views
My example is below:
// 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 );
});
Do I need to include the value
query?
1 Answer
Reset to default 1Assuming that you want to get posts with that meta key, regardless of content, and order by that meta value, you would need to set two properties.
- meta_query
- orderby
So the code, based of your question would be like this
// 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
]);
});
本文标签: wp queryCustom metaquery order for Elementor based on post meta key
版权声明:本文标题:wp query - Custom meta_query order for Elementor based on post meta key 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741235945a2363014.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论