Sort Posts by Most Popular in Oxygen Builder

YouTube video

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. 
  • This field is for validation purposes and should be left unchanged.
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!
register

More Posts

Perfmatters Improved My Google Page Speed by 145%

I'm a bit embarrassed to say this, but my page speed scores on my own agency website and the mobile version of the Permaslug site sucked until just 24 hours ago. (If you don't want…
READ post

Multi-User Oxygen Editing Plugin - Collaboration by dPlugins

A long requested feature for Oxygen has been allowing multiple users to work inside Oxygen at the same time, which unfortunately is not a core feature. Thankfully the team at dPlugins has created a solution…
READ post

Meta Box and Oxygen Builder Deep Dive

With the release of Oxygen 3.9 comes direct integration with Meta Box, a custom field solution for WordPress. In this stream, we'll take a deep look into all the settings and how to utilize it…
READ post
menu linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram