• Resolved ahh_wang

    (@ahh_wang)


    Hi,

    I’m trying to sort the posts by most popular. I want to sort by the amount of page views so that the posts with the most views show up first.

    I am not sure what to put into these fields – https://prnt.sc/ely3fa

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author PickPlugins

    (@pickplugins)

    Thanks for your post.

    If you used meta field for store post view count you can use that meta key under

    Post query orderby > Meta value(Number)
    And put the meta key under meta_key field

    Please see the attachment

    View post on imgur.com

    You don;t need use meta query parameter

    Regards

    Thread Starter ahh_wang

    (@ahh_wang)

    Thank you for your reply.

    Still not working ??

    https://prntscr.com/em345x

    I’m trying to get it to work here – https://belt.life/test-popular-posts/

    Plugin Author PickPlugins

    (@pickplugins)

    Guys you didn’t understand clearly.

    i can see you put view_count in meta key field that should be different that provide 3rd party plugin.

    you will need to use 3rd party plugin for view count that store “View count” under post meta field.

    Regards

    Thread Starter ahh_wang

    (@ahh_wang)

    Ahh okay I think I understand. Do you have a plugin you would recommended?

    Plugin Author PickPlugins

    (@pickplugins)

    Let me search for that.

    Regards

    Thread Starter ahh_wang

    (@ahh_wang)

    Thank you so much. I really appreciate it. If you can find a plugin and then let me know which value to enter https://prntscr.com/em3enn I will be so happy! Thank you.

    Plugin Author PickPlugins

    (@pickplugins)

    Hi, i haven’t found any plugin that store view count under post meta,

    Please follow the step:

    You need to add following code to your theme functions.php

    
    remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
    add_action('wp_head','setPostViews');
    
    function setPostViews() {
    	
    	if(is_singular()){
    		
    		$postID = get_the_id();
    		
    		$count_key = 'post_views_count';
    		$count = (int)get_post_meta($postID, $count_key, true);
    		
    		$count +=1; 
    		update_post_meta($postID, $count_key, $count);
    
    		}
    }
    

    And then visit some of your post,
    AT post grid settigns page as i told before please use post_views_count as meta_key

    Please follow the screenshot

    View post on imgur.com

    you can also display this view count at post grid via layout editor

    View post on imgur.com

    View post on imgur.com

    Regards

    Thread Starter ahh_wang

    (@ahh_wang)

    Okay great! I hope this will work but actually when I add the code to functions.php I get an error.

    This is all of the code from the functions.php child theme

    <?php 
    
    add_action( 'wp_enqueue_scripts', 'my_enqueue_assets' ); 
    
    function my_enqueue_assets() { 
    
        wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' ); 
    
    }
    
    remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
    add_action('wp_head','setPostViews');
    
    function setPostViews() {
            
            if(is_singular()){
                    
                    $postID = get_the_id();
                    
                    $count_key = 'post_views_count';
                    $count = (int)get_post_meta($postID, $count_key, true);
                    
                    $count +=1; 
                    update_post_meta($postID, $count_key, $count);
    
                    }
    }

    https://prntscr.com/em405q

    When I update I get this error – https://prntscr.com/em40gz

    Thread Starter ahh_wang

    (@ahh_wang)

    Ohh wait! I added it to the parent theme and it’s working!!

    Thank you so much! Amazing support.

    Plugin Author PickPlugins

    (@pickplugins)

    Glad to see that help you.

    If you love the support and our plugins please submit us five-star reviews at www.ads-software.com plugin page https://www.ads-software.com/support/plugin/post-grid/reviews/

    Regards

    Thread Starter ahh_wang

    (@ahh_wang)

    Already did! ??

    Cheers!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Sort by Post View Count’ is closed to new replies.