• Resolved casperknight

    (@casperknight)


    I have Elementor and wants to query wordpress popular posts plugin with one of Elementor’s posts’ widgets instead of WPP’s css formatting to match the rest of the website design.

    I read the sticked post, but didn’t understand the “Create WP Query” part? the first code I put it in functions.php, what about the second code? where I put it exactly?

Viewing 7 replies - 16 through 22 (of 22 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    @zydarek have a look at my first comment, that should get you on the right track.

    Edit: Just to clarify, it seems you need to use Elementor’s Posts widget for all this to work (at least according to OP’s comments).

    • This reply was modified 3 years, 5 months ago by Hector Cabrera. Reason: Added note about Elementor's Posts widget

    @casperknight Would you mind writing exactly what you did please, step by step? I’m not code save at all, but need to do the exact same thing as you did.
    We use Elementor one this page for top posts from last 24h https://cakeroom.pl/galeria/top-dnia/ ,and need to display the results via elementor. I’d greatly appreciate your help. Thank you! Magda

    For me it doesn’t work for what i did;
    1. I added to my child theme functions.php this code:

    function custom_wpp_update_postviews($postid) {
        $accuracy = 100;
    
        if ( function_exists('wpp_get_views') && (mt_rand(0,100) < $accuracy) ) {
                   update_post_meta(
                $postid,
                'views_daily',
                wpp_get_views($postid, 'daily', false)
            );
        }
    }
    add_action('wpp_post_update_views', 'custom_wpp_update_postviews');
    
    add_action('elementor/query/popular_posts', function($query) {
    	$query->set('orderby', 'meta_value_num');
    	$query->set('meta_key', 'views_daily');
    });
    

    2. I use elementor posts widget and in custom query id put “popular_posts”

    Its something more what i need to do?

    firstly, I want to thank @hcabrera for the plugin and awesome support and also @casperknight for initiating and posting the code.

    @zydarek @akademiatortu I can confirm this worked for me. I’ll add a step-by-step as my little way of helping in this thread:

    1) install code snippets plugin (unless you are comfortable adding code to your theme)
    2) copy and paste this code into a new snippet and then save changes and activate:

    function custom_wpp_update_postviews($postid) {
        // Accuracy:
        //   10  = 1 in 10 visits will update view count. (Recommended for high traffic sites.)
        //   30  = 30% of visits. (Medium traffic websites.)
        //   100 = Every visit. Creates many db write operations every request.
    
        $accuracy = 50;
    
        if ( function_exists('wpp_get_views') && (mt_rand(0,100) < $accuracy) ) {
            // Remove or comment out lines that you won't be using!!
            update_post_meta(
                $postid,
                'views_total',
                wpp_get_views($postid, 'all', false)
            );
            update_post_meta(
                $postid,
                'views_daily',
                wpp_get_views($postid, 'daily', false)
            );
            update_post_meta(
                $postid,
                'views_weekly',
                wpp_get_views($postid, 'weekly', false)
            );
            update_post_meta(
                $postid,
                'views_monthly',
                wpp_get_views($postid, 'monthly', false)
            );
        }
    }
    add_action('wpp_post_update_views', 'custom_wpp_update_postviews');
    
    add_action('elementor/query/popular_posts', function($query) {
    	$query->set('orderby', 'meta_value_num');
    	$query->set('meta_key', 'views_daily');
    });

    3. add “popular_posts” into the Elementor Posts widget

    4. make sure you have date set to “all” and order as “DESC”

    5. depending on how much traffic your site gets, you may need to tweak the “sample rate” in the Popular Posts plugin (in “tools” section of plugin settings). If you have low traffic and want to test it is working, set the sample rate to 1 and then open up a few posts in an incognito window. clear any plugin caches you use and check the page where the elementor posts display. hopefully it works for you like it did for me!

    Hello – if I understood correctly this is to use WPP in the custom requests of the post elementor widget. To fuel your thinking (I’m no expert)

    1 – Is the proposed function heavier than a shortcode in terms of page loading?

    FYI I also use a function so that the WPP shortcode also works on the current category pages (elementor model) and another to limit the display to articles less than a year since their publication

    2 – How to be able to use WPP in metadata [wpp_counter_view] of each post such as for example the date (list of posts etc …) in each elementor widget?

    For information I also use a function so that the counter is of a different style and color according to the level of the counter of views

    Thank you
    I agree with you on the praise for the plugin designer

    Thread Starter casperknight

    (@casperknight)

    Again, I apologize for the very late reply as I rarely even login in www.remarpro.com

    I did what exactly @robhills1985 said. There’s nothing more I can really add, he explained the steps accurately.

    Cheers everyone!

    Michael

    (@wallpaperman)

    Hello and first of all thanks to @hcabrera for this plugin and the great support and to all the people her for finding a solution. I use this solution as @robhills1985 explained it and it works fine for the one query, but I want to use different queries for different elementor pages.

    What do I have to do, to realize that. I added the following code with a second query called “popular_weekly” but this didn′t work.

    add_action('wpp_post_update_views', 'custom_wpp_update_postviews');
    
    add_action('elementor/query/popular_posts', function($query) {
    	$query->set('orderby', 'meta_value_num');
    	$query->set('meta_key', 'views_daily');
    
    add_action('elementor/query/popular_weekly', function($query) {
    	$query->set('orderby', 'meta_value_num');
    	$query->set('meta_key', 'views_weekly');
    });

    Thanks for any help in advance.

Viewing 7 replies - 16 through 22 (of 22 total)
  • The topic ‘How to query WPP with Elementor?’ is closed to new replies.