-
Hello,
I tried the following but it doesn’t work as I expected.
What I want to do:
Add a Parameter of “order by popularity” to “ORDER BY” setting in Query Loop so that posts will be arranged in a popular order based on the number of views.What I have done (1 through 3 below):
1. Installed the plugin “Post Views Counter” to get the number of views for each post.2. Added the following PHP snippet and activated.
add_filter( ‘generateblocks_query_loop_args’, function( $query_args, $attributes ) {
// apply filter if loop has class: orderby-pvc-views
if (! is_admin() && ! empty( $attributes[‘className’] ) && strpos( $attributes[‘className’], ‘orderby-pvc-views’ ) !== false) {$query_args = array_merge( $query_args, array(
‘meta_key’ => ‘_pvc_views’,
‘orderby’ => ‘meta_value_num’,
‘order’ => ‘ASC’,
));
}return $query_args;
}, 10, 2 );
3. Set “orderby-pvc-views” in ADDITIONAL CSS CLASS(ES) of the advanced setting of the Grid.
The issue I have:
The parameter of “popularity” is not added in the ORDER BY setting, though other default parameters like Id, Title, Slug and others are shown.I guess that there is something wrong with the snippet I added. But I can’t see anything I have to do.
I would appreciate your support.
-
Alvind
Hi there,
Regarding your issue, the query loop filter does NOT add a new option under the parameter dropdown setting. The setting is already applied in the code, so you can only see the changes on the frontend.
Just make sure the
meta_key
value is correct for it to work. -
Hello,
Thank you for your reply. I got it.
I appreciate your support.
-
Alvind
No problem, glad to hear that 🙂
- You must be logged in to reply to this topic.