Easy Queries For Genesis

Powerful WordPress queries -- made easy.

Our plugin Easy Queries is an excellent tool for those situations where your client asks for something like, I need a page with all the posts in the special events category, or all products in the product category pants, or the product category shoes where the price is less than $10. This is the sort of thing WordPress is good at, organizing content by taxonomies (such as categories) and custom fields.

Telling WordPress what content you want isn’t always easy. Easy Queries gives you a visual editor that makes finding exactly the right content simple, while giving you real time feedback on what the results of your queries are.

While Easy Queries has its own templating system for displaying your results — two of them really — you may want the results to look just like any other post archive on your site. With the Genesis Framework, this is fairly simple to do.

In this tutorial I’ll be showing you a simple child theme I made for Genesis, that when you have a page with the same slug, as an Easy Query, that page will be magically transformed into an archive for those posts. Those posts can be standard posts, or from a custom post type, or some combination of multiple post types.

I posted a complete child theme that you can install and use on Github. Feel free to integrate its page.php template into your own Genesis child theme, or fork this starter theme and do what you want with it.

Using An Easy Query With The Genesis Loop

My child theme has only one custom template — page.php. For my testing purposes I created an Easy Query called foods, I guess because I was hungry when I started working on this whole thing. In my Easy Query, I set it to show posts┬áthat are in one of two categories “food” and “snacks”.

In addition, I asked Easy Queries to query in both the default post type, and in a custom post type. I also ordered my posts alphabetically.

Then I created a page and made sure its slug was foods. Then as my proof of concept, this is what I did, in my child theme’s page.php:

<?php remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'slug_easy_queries_poc' ); function slug_easy_queries_poc() { 	 	if ( is_page( 'foods' ) ) { 		//get class instance 		$class =  \calderawp\caeq\core::get_instance(); 		//Get WP_Query args for the "foods" Easy Query 		$args =  $class->build_query_args( 'foods' );
		
		//do genesis custom loop
		genesis_custom_loop( $args );
	}else{
		
		//do the standard loop if not on the right page
		genesis_standard_loop();
	}
	
	
}
genesis();

What I’m doing here is checking if I’m on the page “foods” and if so, replacing the WP_Query arguments with the arguments from my Easy Query, and then letting the function genesis_custom_loop take care of the rest.

I got the arguments from my Easy Query using the method build_query_args of the main class of Easy Queries. This method’s job is to take the slug or ID of any Easy Query and turns it into WP_Query arguments. It is used internally for rendering Easy Queries using the Easy Queries, but it is a public method, so it can get used separately from Easy Queries’ templating system.

This proof of concept works and if you only want to pull this trick once, may be all you need. That said, I wanted something that would work with any combination of pages and Easy Queries with the same slug.

Show Any Easy Query

My final product takes any Easy Query, with the same name as a page and turns it into an archive. This way all I have to do is create a page whose slug matches the slug of my Easy Query and I have a front-end view of all posts matching that query, that looks exactly like any other archive.

Keep in mind that page slugs in WordPress use dashes, while the slug of an Easy Query uses underscores. That’s easily adjusted for, which I will show below.

Querying by taxonomy, especially multiple taxonomies, is a great use for this type of system as it helps you solve those requirements that WordPress doesn’t provide by default. Don’t forget your query can also be based on custom fields. This is really useful when you’re employing a custom fields plugin like Advanced Custom Fields or Pods. Combined with one of those plugins you have a UI for saving custom fields, and querying by them.

My final page.php, which you can see in the child theme, or below, is very similar to the proof of concept I showed above.

The difference is that it sets the Easy Query slug using the current page name. Since build_query_args will return false when there is no Easy Query with the slug passed to it, we can test what it returns. If its false, we fall back to the standard loop. If it returns an array of arguments, then we pass those arguments to genesis_custom_loop like before.

Here is the code for the page.php:

remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'cwp_genesis_easy_queries_do_custom_loop' );
function cwp_genesis_easy_queries_do_custom_loop() {

	//get slug of current page
	$page = get_query_var( 'pagename' );

	//Correct for the fact that Easy Queries have underscores, not dashes
	$slug = str_replace( '-', '_', $page );

	//get class instance
	$class =  \calderawp\caeq\core::get_instance();

	//find arguments for an Easy Query with same slug as page
	$args =  $class->build_query_args( $slug );

	//check if we found a matching Easy Query
	if ( ! $args ) {

		//Nope, so do normal loop.
		genesis_standard_loop();
	}else{

		//Found an Easy Query, use its arguments
		genesis_custom_loop( $args );
	}

}

//Do some Genesis
genesis();