Using Your Theme’s Defaults For Easy Query Results

Powerful WordPress queries -- made easy.

Our plugin Easy Queries, lets you create custom collections of posts — optionally using a search form — and display them using a template you define with our integrated template editor. The template editor is designed to be simple and easy to use, with no need to write any PHP code.

That’s pretty cool, but what if you want the results to look exactly like they do in your theme? Because every theme is different we can’t make a button to do that, but it’s still doable. I previously showed how to make a Genesis child theme that converts a page to a Easy Queries defined post archive. In that example, I used Easy Queries to generate the query and then rendered it with genesis_custom_loop().

Today I want to show you two examples, one using get_template_part() and one using a custom function from a theme. The first case works with Twenty Fifteen but should be adaptable to a lot of themes. The second example uses a Themify Builder element — it’s a good example of integration with a theme.

Note: This is a little tricky to do. I know that using Easy Queries this sort of way makes it “Slightly Tricky Queries,” but keep in mind that you’re paying for support and we are here to help. In fact, this article is based on one of our user’s request to make this work with Themify. I helped him and then wrote this up, based on what I made for him.

How It Works

Easy Queries has a filter called “caldera_easy_queries_loop_callback” that defines what function is used to render the template results. By default, an internal function is called to render the template defined in the plugin admin.

You can use this filter define any function to render your Easy Query. That function will be passed the WP_Query object that you can then loop through.

So, to make this work, you need two functions. The first is hooked to “caldera_easy_queries_loop_callback” and tells Easy Queries to call the second function. Then the second function does the loop. You can make your own custom template there, which allows you to either write your template in PHP, or call a template part or another function.

Using A Template Part

The first example is designed to work with Twenty Fifteen. It replicates how many themes use get_template_part() to include a separate file for the post content. This example should be adaptable to how many themes work. You might need to tweak the arguments for get_template_part() to make it work.

First we will need a function hooked to “caldera_easy_queries_loop_callback” to call our “looping” function. I’ll call the first function “slug_set_ceq_callback” and the looping function “slug_ceq_custom_template_part_loop” but you should change “slug” to a custom slug for your child theme.

Open up your child theme’s functions.php and add this:

add_filter( 'caldera_easy_queries_loop_callback', 'slug_set_ceq_callback', 10, 2 );
function  slug_set_ceq_callback( $cb, $slug ) {
	//MAKE SURE TO CHANGE SLUG!
	if( 'your-easy-queries-slug' == $slug && function_exists( 'themify_get_shortcode_template') ) {
		$cb = 'slug_ceq_custom_template_part_loop';

	}

	return $cb;

}

Notice that this change is conditional based on the slug of the Easy Query. You need to change that to the slug of the Easy Query you want this to apply to, or you get rid of the conditional to make it global.

Now we’ve told Easy Queries what function to call with $cb = 'slug_ceq_custom_template_part_loop';. Now we need to make that function, which loops through the post collection created by Easy Queries:

function slug_ceq_custom_template_part_loop( $query ) {
	if( $query->have_posts() ) {
		while ( $query->have_posts() ) {
			$query->the_post();
			get_template_part( 'content', get_post_format() );
		}

	}else{
		get_template_part( 'content', 'none' );
	}

}

This function looks very much like Twenty Fifteen’s index.php — that’s because it does the same thing. With it, you can make your Easy Query results look just like Twenty Fifteen’s blog index or archives.

Using A Theme’s Function For Your Results

As I said earlier, this post is inspired by a user I helped. He wanted search results to look exactly like what a Themify Builder element creates. The best way to do that is to use the same function as the Themeify shortcode does for rendering the post.

The first half of this scenario is the same as before. We use the “caldera_easy_queries_loop_callback” to create a custom callback function. We couldn’t just use the function from Themify as we need to pass it an array of posts, not a WP_Query object, so we still need our own function. Here is how it worked:

add_filter( 'caldera_easy_queries_loop_callback', 'slug_set_loop_cb', 10, 2 );
function  slug_set_themify_loop_cb( $cb, $slug ) {
	//MAKE SURE TO CHANGE SLUG!
	if( 'your-easy-queries-slug' == $slug && function_exists( 'themify_get_shortcode_template') ) {
		$cb = 'slug_do_loop_with_themify';

	}

	return $cb;

}

function slug_do_loop_with_themify( $query ) {
	if ( $query->have_posts() ) {
		echo themify_get_shortcode_template( $query->posts );
	}

}

As I said, this is similar. The difference is that we didn’t do the loop ourselves. We let themify_get_shortcode_template do the loop for us, we just passed it the posts from our Easy Queries’ generated WP_Query object.