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
        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
add_action( 'wp_head', 'permaslug_track_post_views');

Query used in Repeater:



Learn more about my Oxygen Builder course here:

Visit the premium 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


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
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram