Display a selection of entries in custom order with Osom Blocks

display-selection-CPT-entries-Osom-Blocks

Discover how you can display a selection of entries in a custom order in a block with Osom Blocks plugin, with a little bit of code.

Displaying entries from Custom Post Types using a block is not easy, since native blocks only support entries and pages. That’s why at OsomPress we created the plugin Osom Blocks.

This one allows you to create a listing of any post type (CPTs, posts and pages) and customize it using the user interface. Among the settings are:

  1. Custom content type.
  2. Number of entries.
  3. Show/hide the featured image.
  4. Show/hide excerpt.
  5. Choose the grid layout.
  6. Choose the number of columns for the grid layout.
  7. Choose the HTML markup for the title (H2, H3 or H4).
  8. Customize the “Read more” text.
  9. Show/hide parent entries/pages (only in hierarchical custom content types).
  10. Show/hide pagination.
screenshot-osom-blocks

But you may have to modify the query beyond these options on occasion.

The idea is to keep the plugin simple to use, so we don’t want to clutter the panel with too many options.

So we have added (in version 1.2) a PHP filter that allows you to access the block query and modify it easily.

This allows you to combine it with conditional tags to get, for example, a selection of entries displayed on a particular page and in a custom order.

Let’s see how:

Snippet to display a selection of entries in custom order with Osom Blocks

Add an Osom Blocks block and configure the settings to your liking. The “Select content type” section you can leave it as it is, as this is the part we are going to modify using the filter.

Now, add the following code at the end of functions.php or in your functionality plugin:

// Show chosen entries in custom order with Osom Blocks
add_filter( 'osom_blocks_args', 'cg_display_selected_posts' );
function cg_display_selected_posts( $args ) {
	if ( is_page(78) ){
		$args['post_type'] = array('post');
		$args['posts_per_page'] = '3';
		$args['post__in'] = array('37743', '717', '2274');
	}
	return $args;
}

In this code you use the osom_blocks_args filter to modify the query and ask it to display the 'post'entries in the page with ID=78 ( is_page(78) ) of the website.

You can modify both the post type by any CPT or ‘page’ you are interested in and use another conditional tag (e.g. is_home, is_front_page, is_archive…).

Also, you set the number of entries to be displayed to make sure they are the same as the ones you have added in the post__in array.

You can see that having a filter that gives you access to the query gives you extra flexibility. 

Keep in mind that since you are using a PHP filter, you have the limitation that you will only see the result in the frontend of the web, in the backend the changes will not be reflected.

Conclusions

You see how easy it is to modify the query of an Osom Blocks block to display a selection of entries in the order you want. 

Filed under

,

Nahuai Badiola Avatar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *