Limited Time Sale - 10% Discount! Use Code:U5ZBQTEY

Using a Custom Query to Load Posts or Custom Post Types

Our plugins make it really easy to query posts and custom post types from within our modules. With a few clicks, you can select which post types you want to include along with related categories and tags. But we’ve come to realize that for some, there is still a need for more complex queries that can’t be created from the module settings. For this reason, we’ve added a custom query filter.

Custom Query Filter

For the Portfolio Posts Pro plugin, the filter name is dp_ppp_custom_query_args. For the Owl Carousel Pro plugin, the filter name is dp_ocp_custom_query_args. To implement them, you’ll need to add the correct filter to your functions.php file within your child theme. To display the results from this query, just turn on the Custom Query option within the module settings. This will ignore all of the other query options within the module (Post Type, Categories, Tags, etc.)  Here is a basic example:

function dp_ppp_custom_query_function() {

return array(
'cat' => '9,12,16',
);
}

add_filter('dp_ppp_custom_query_args', 'dp_ppp_custom_query_function');

Again this is a very basic query. We are simply querying posts from three categories using the category IDs. The function dp_ppp_custom_query_function is returning our array of arguments and passing them to the custom query filters. You can change the name of this function to anything you like. The filter name however must be dp_ppp_custom_query_args.

By default, the filter will display the 10 most recent blog posts. If you turn the Custom Query option on in the module but do not add the filter to your functions.php file, you should see the 10 most recent blog posts. Even if you add the filter but have an error in your argument array, it will likely display the default filter results. Keep this in mind when troubleshooting. When in doubt, copy and paste the function above to test and use your category IDs.

Displaying posts from the current post category

The following example shows how you can display posts from the same category as the current post, and exclude the current post from the result:

function dp_ppp_custom_query_function() {

	$currentID = get_the_ID();
	$current_categories = get_the_category();

	foreach($current_categories as $current_category) {
		$category[] = $current_category->term_id;
	}

	$categories = implode(",",$category);

	return array(
		'cat' => $categories,
		'post__not_in' => array($currentID),
	);

}

add_filter('dp_ppp_custom_query_args', 'dp_ppp_custom_query_function');

Displaying posts from the current custom post type category

This example is very similar to the example above, but works with custom post types and custom taxonomies. We are also using the Divi FilterGrid custom query function in this example:

function dpdfg_custom_query_args() {
    $current_post_id = get_the_ID();
    $terms_of_current_post = get_the_terms($current_post_id, 'project_category');
    $terms_ids = array();
    foreach ($terms_of_current_post as $term_object) {
        $terms_ids[] = $term_object->term_id;
    }
    return array(
        'post_type' => 'project',
        'post_status' => 'publish',
        'posts_per_page' => 12,
        'post__not_in' => array(
            $current_post_id
        ),
        'tax_query' => array(
            array(
                'taxonomy' => 'project_category',
                'field' => 'term_id',
                'terms' => $terms_ids,
                'operator' => 'IN'
            )
        )
    );
}

add_filter('dpdfg_custom_query_args', 'dpdfg_custom_query_args');

Here is an example of a more complex query:

function dp_ppp_custom_query_function() {
	return array(
    'post_type' => 'post',
    'tax_query' => array(
		'relation' => 'OR',
		array(
			'taxonomy' => 'category',
			'field'    => 'slug',
			'terms'    => array( 'quotes' ),
		),
		array(
            'relation' => 'AND',
            array(
				'taxonomy' => 'post_format',
				'field'    => 'slug',
				'terms'    => array( 'post-format-quote' ),
			),
			array(
				'taxonomy' => 'category',
				'field'    => 'slug',
				'terms'    => array( 'wisdom' ),
			),
		),
	),
);
}

add_filter('dp_ppp_custom_query_args', 'dp_ppp_custom_query_function');

You can find more examples of complex queries in the WordPress Codex.

 

Using different queries on multiple pages

You may run into a situation where you need to run one custom query on one page but a different custom query on another. In this case you’ll need to get the current post or page ID and use a different filter function depending on the ID. In the example below we are:

– getting the ID of the page

– adding a different function to the custom query filter based on the current page ID

– and finally creating different query arguments in separate functions

In our example below, we are returning a single page in our query arguments instead of posts, but you could replace the page_id argument with category IDs like the first example above.

add_action( 'wp', 'dp_ppp_custom_query_function' );

function dp_ppp_custom_query_function() {
	$postID = get_the_ID();
	if( $postID == '25174' ) {
		add_filter('dp_ppp_custom_query_args', 'dp_ppp_custom_query_page1');
	} elseif( $postID == '26202' ) {
		add_filter('dp_ppp_custom_query_args', 'dp_ppp_custom_query_page2');
	}
}

function dp_ppp_custom_query_page1() {
	return array(
		'page_id' => 292,
	);
}

function dp_ppp_custom_query_page2() {
	return array(
		'page_id' => 499,
	);
}

The Owl Carousel custom query option works exactly the same way as above, you’ll just need to use this filter dp_ocp_custom_query_args. Here is an example:

function dp_ocp_custom_query_function() {

return array(
'cat' => '9,12,16',
);
}

add_filter('dp_ocp_custom_query_args', 'dp_ocp_custom_query_function');

Remember, the filter will display the 10 most recent blog posts by default. If you are unable to get results other than this, there is an error in your arguments array or you forgot to turn on the custom query option in the module.

Pin It on Pinterest

Share This