Register

Sort Posts by Most Popular in Oxygen Builder

This tutorial video will show you how you can easily sort posts by most popular (most viewed) in Oxygen Builder. Using a few custom code snippets, we can easy add a meta key field that the Repeater element uses to sort posts. This is great for local directory or real estate sites where you might want to give a sense of urgency to how many times a page or post has been viewed.

Add the below code as 2 snippets using the Code Snippets WordPress plugin.

Posts views count function:

function permaslug_set_post_views($postID) {
    // Define meta field
    $post_count = 'permaslug_post_views_count';
    // Get meta field
    $count = get_post_meta($postID, $post_count, true);
    // If the count is empty
    if($count == '') {
        // Set the count to 0
        $count = 0;
        delete_post_meta($postID, $post_count);
        add_post_meta($postID, $post_count, '0');
    } else {
        // Increment the count by one
        $count++;
        update_post_meta($postID, $post_count, $count);
    }
}
//To keep the count accurate, remove prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

Count single posts:

function permaslug_track_post_views ($post_id) {
    // Only run for single posts
    if ( !is_single() ) return;
    // Set the post ID
    if ( empty ( $post_id) ) {
        global $post;
        $post_id = $post->ID;
    }
    // Run the permaslug_set_post_views function
    permaslug_set_post_views($post_id);
}
add_action( 'wp_head', 'permaslug_track_post_views');

Query used in Repeater:

https://raw.githubusercontent.com/permaslug/Order-by-popular-query/main/manual%20query

 

 

Learn more about my Oxygen Builder course here: https://permaslug.com/ultimate-oxygen-course/

Visit the premium forums: https://permaslug.com/forums/

Signup For Emails

Want to be notified when new tutorials are released? Signup below for our roundup emails. We promise not to spam and won't blow up your inbox. 
Jonathan Jernigan, Founder
Permaslug and APEX Web Solutions

JOIN PERMASLUG

Membership Benefits Include:

  • Ultimate Oxygen Course
  • Ultimate Woocommerce Course
  • ACF Pro Course
  • Material Design Lite
  • Template Downloads
  • Community
  • And more!
Learn MOre

Read More

Powered by 
APEX Web Solutions
menu-circle
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram