We are increasing the price on 14th may 2024. 

Premium add-on for Bricks Builder

Get the most viewed posts with Post Views Count plugin and Bricks

In this tutorial, we used the 3rd party plugin “Post Views Count” plugin for tracking the post views counter. You will install, activate, and setup the settings of the plugin as per your requirement. Here I am giving the compatibility query builder provider for the Bricks builder editor.

Creating Loop Builder Provider

Open the functions.php file of your Bricks child theme or you can use any 3rd party snippets plugin. Enter the following codes:

if ( class_exists( 'Post_Views_Counter' ) ) {
	/**
	 * Adds custom query provider for Post Views Count plugin
	 */
	add_filter( 'bricks/setup/control_options', 'pvc_add_control_options' );
	function pvc_add_control_options( $control_options ) {
		$control_options['queryTypes'][ 'pvcmvp' ] = esc_html__("Most Viewed Posts - PVC", 'bricksultimate');

	    return $control_options;
	}

	add_filter( "bricks/elements/container/controls", 'pvc_add_query_controls' );
	add_filter( "bricks/elements/div/controls", 'pvc_add_query_controls' );
	add_filter( "bricks/elements/block/controls", 'pvc_add_query_controls' );
	function pvc_add_query_controls( $controls ) {

		// get main instance
		$pvc = Post_Views_Counter();

		$pvcControls['pvc_post_type'] = [
			'tab' 			=> 'content',
			'type' 			=> 'select',
			'label' 		=> esc_html__('Post type', 'bricks'),
			'options' 		=> $pvc->functions->get_post_types(),
			'multiple' 		=> true,
			'placeholder' 	=> 'post',
			'inline' 		=> true,
			'required'  	=> [ [ 'hasLoop', '=', true ], [ 'query.objectType', '=', 'pvcmvp' ] ]
		];

	    $pvcControls['pvc_order'] = [
			'tab' 			=> 'content',
			'type' 			=> 'select',
			'label' 		=> esc_html__('Order', 'bricks'),
			'options' 		=> \Bricks\Setup::get_control_options('queryOrder'),
			'placeholder' 	=> 'desc',
			'inline' 		=> true,
			'required'  	=> [ [ 'hasLoop', '=', true ], [ 'query.objectType', '=', 'pvcmvp' ] ]
		];

		$pvcControls['pvc_posts_per_page'] = [
			'tab' 			=> 'content',
			'type' 			=> 'number',
			'label' 		=> esc_html__('Posts per page', 'bricks'),
			'unitless' 		=> true,
			'step' 			=> 1,
			'placeholder' 	=> 5,
			'inline' 		=> true,
			'required'  	=> [ [ 'hasLoop', '=', true ], [ 'query.objectType', '=', 'pvcmvp' ] ]
		];

		$pvcControls['exclude_sticky_posts'] = [
			'tab' 			=> 'content',
			'type' 			=> 'checkbox',
			'label' 		=> esc_html__('Exclude sticky posts', 'bricks'),
			'inline' 		=> true,
			'required'  	=> [ [ 'hasLoop', '=', true ], [ 'query.objectType', '=', 'pvcmvp' ] ]
		];

		$pvcControls['include_ids'] = [
			'tab' 			=> 'content',
			'type' 			=> 'text',
			'label' 		=> esc_html__('Include posts', 'bricks'),
			'placeholder' 	=> esc_html__('Enter post ids with comma', 'bricks'),
			'required'  	=> [ [ 'hasLoop', '=', true ], [ 'query.objectType', '=', 'pvcmvp' ] ]
		];

		$pvcControls['exclude_ids'] = [
			'tab' 			=> 'content',
			'type' 			=> 'text',
			'label' 		=> esc_html__('Exclude posts', 'bricks'),
			'placeholder' 	=> esc_html__('Enter post ids with comma', 'bricks'),
			'required'  	=> [ [ 'hasLoop', '=', true ], [ 'query.objectType', '=', 'pvcmvp' ] ]
		];

		$array_search = absint( array_search( 'query', array_keys( $controls ) ) );
		$controls = array_slice( $controls, 0, $array_search + 1, true ) + $pvcControls + array_slice( $controls, $array_search + 1, null, true );

	    return $controls;
	}

	add_filter( 'bricks/query/run', 'pvc_run_query', 15, 2 );
	function pvc_run_query( $results, $query ) {
		if( $query->object_type != 'pvcmvp' ) {
			return $results;
		}

		$settings = $query->settings;
		$args = [];
		foreach( ['post_type', 'order', 'posts_per_page' ] as $value ) {
			if( isset( $settings['pvc_' . $value ] ) ) {
				$args[ $value ] = $settings['pvc_' . $value ];
			}
		}

		$exclude_posts = [];
		if( isset( $settings['exclude_sticky_posts'] ) ) {
			$exclude_posts = get_option( 'sticky_posts' );
		}

		if( isset( $settings['exclude_ids'] ) ) {
			$exclude_posts = array_merge_recursive( $exclude_posts, explode( ',', $settings['exclude_ids'] ) );
		}

		$args['exclude'] = $exclude_posts;

		if( isset( $settings['include_ids'] ) ) {
			$args['include'] = explode( ',', $settings['include_ids'] );
		}

		$mvp = pvc_get_most_viewed_posts( $args );

		return ( $mvp ) ? $mvp : $results;
	}

	add_filter( 'bricks/query/loop_object', 'pvc_loop_object', 10, 3 );
	function pvc_loop_object( $loop_object, $loop_key, $query ) {
		if( $query->object_type == 'pvcmvp' ) {
			global $post;

			$post = get_post( $loop_object );
			setup_postdata( $post );

			return $post;
		}

		return $loop_object;
	}
}

This code will create the custom provider for the Bricks loop builder. You can easily make a flexible layout for your content. Checkout the video below:

Fills In: 

Leave the first comment

Search Tutorials

Request TutorialsFeatures Request