Skip to content

Alter The Related Posts Query

The Total theme includes a related posts section for your portfolio items, staff member pages and your blog posts, this section displays more items from the same post type that are included in the same category(ies) as the current post. These queries can be altered very easily via a child theme. Have a look at the included theme filters below and a few examples of how to use them.

Important: In Total 5.0+ you can now find settings in the Customizer (or post types unlimited plugin for your custom types) so you can select the Related query term, order and orderby settings. You can still use the filters mentioned below for more advanced edits but for most this shouldn’t be needed. Additionally since 5.0+ the theme will check for the Yoast SEO primary category for displaying related items.

Related Posts Theme Filters

Below are the various filters included in the theme, it should be very obvious which filter is used for what.

  • wpex_related_staff_args
  • wpex_related_portfolio_args
  • wpex_blog_post_related_query_args

Example Usage

Here is an example showing how you would alter the related queries via a child theme. This example shows you how to alter the related blog posts query to alter the related portfolio or staff query simply change the filter in the add_filter action.

// Alter the related blog posts query
// IMPORTANT: In Total 5.0+ we added settings to make these changes via the Customizer and Post Types Unlimited plugin.
add_filter( 'wpex_blog_post_related_query_args', function( $args ) {
	
	// Remove category conditional from related blog posts
	unset( $args['tax_query'] );

	// Change order to random
	$args['orderby'] = 'rand';
	
	// Return args
	return $args;

} );
Back To Top