Support is closed now. We shall check all on 20th May 2024.

Premium add-on for Bricks Builder

Display popular posts in bricks

We will display the popular posts(most viewed posts) with the bricks loop builder. If you don’t want to use a plugin, then you can use this method.

Storing Views Counter in The Custom Field

Open the functions.php of the bricks child theme or drop the following code into the 3rd party snippets plugin.

function bu_set_post_views($postID) {
    $count_key = 'wpbu_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
//To keep the count accurate, lets get rid of prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

function bu_update_post_views($post_id) {
    //* returning early if it is not single post page
    if ( ! is_singlar('post') ) return;
    if ( empty ( $post_id) ) {
        global $post;
        $post_id = $post->ID;    
    }
    bu_update_post_views($post_id);
}
add_action( 'wp_head', 'bu_update_post_views');

Once you have used this code, every time a user visits the single post page, the custom field wpbu_post_views_count will be updated. 

Note: If you are using a caching plugin, then this method will not work by default. You could use the Fragmented Caching feature that’s offered by some advanced caching plugins to bypass the caching plugins.

Display Popular Posts with Loop Builder

BrciksUltimate has a custom wp-query loop builder. With this feature, we shall create a flexible post listing layout and display the popular posts on the site. Activate the Ultimate Query Builder option from your Dashboard -> Bricks -> BricksUltimate -> Misc tab. Now add one element of these container, block, and div elements to the canvas and enable the loop option. Select the Query -> Type -> Ultimate Query Builder -> Custom WP Query provider.

Drop the following PHP code in the code text box and create the post layout with the help of the nested elements like image, post title, excerpt, button, etc.

<?php
$args = array( 
	'posts_per_page' => 4, 
	'meta_key' => 'wpbu_post_views_count', 
	'orderby' => 'meta_value_num', 
	'order' => 'DESC'  
);

return $args;

Display Post View Counter

You can display the post view count on your single post pages with the bricks’ dynamic tag option. Add this code into the functions.php of the bricks child theme or a 3rd party snippets plugin.

function bu_get_post_views(){
	$postID = get_the_ID();
    $count_key = 'wpbu_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}

Now open the bricks builder editor and use the Basic Text or any other element and use this dynamic tag { echo:bu_get_post_views() }

Fills In: 

4 comments

  • When I put the first code “Storing Views Counter” in functions.php file, all pages turned into blank.

  • Igor Poslov

    Will this method count bots? Or bots can be excluded somehow?

Leave your comment

Search Tutorials

Request TutorialsFeatures Request