Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Add a Parameter of “order by popularity” to “ORDER BY” setting in Query

  • 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.

  • 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.

  • No problem, glad to hear that 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.