Skip to content

Alter The Related Posts Query

In this article

    The Total theme comes with a related posts section for portfolio items, staff member pages, and blog posts. This section automatically displays additional items from the same post type that share one or more categories with the current post.

    These queries are fully customizable through a child theme. Below, you’ll find the built-in theme filters along with some examples of how to use them.

    Related Posts Settings in Total 5.0+

    Important: Starting with Total 5.0, you can configure related posts directly in the Customizer (or via the Post Types Unlimited plugin for your custom post types). This allows you to select the related query term, as well as the order and orderby settings.

    For most users, these options are sufficient, and you won’t need to use the filters described below. However, the filters are still available for more advanced customizations. Additionally, since version 5.0, the theme will automatically check for the Yoast SEO primary category when determining which related items to display.

    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;
    
    } );
    Related Articles
    Back To Top