Custom Queries

When working with many of the framework’s elements that pull posts — like within the layout builder, shortcodes, and page templates — there are handy options to pass in simple, custom query strings. And for those that have basic knowledge of WP_Query, this allows you to drill down past the limited options available within these elements, to query the posts in your own way — maybe by a tag, a custom post type, a custom taxonomy, etc.

These custom query options are really handy, if you’re looking to pull posts, from custom scenarios beyond the scope of the framework’s options. However, you will still need basic knowledge of WordPress’s WP_Query parameters. — So make sure to bookmark that article!

Where the Custom Query Options Exist

You can implement custom query string from your WordPress admin through the following framework elements.

Layout Builder

Within most of the elements that display posts, there will be an option asking how you want to pull the posts. And selecting “Custom Query” should bring up a box to input a custom query string.

Shortcodes

When using shortcodes that display posts — like [blog], [post_grid], [post_list], etc. — you can pass in a query parameter.

[post_list query="post_type=book&author=hemingway"]

Page Templates

And finally, when using page templates that display posts — like Blog, Post Grid, Post List, etc. — you can input a custom query string within the Post Template Options meta box. This is also the same as adding a custom field named query.

Limitations

Now the problem with this system is that it can only take you so far. From within your admin, you can only input a custom query as a string, and you cannot actually write out a PHP array.

For example, this will work:

post_type=book&author=hemingway
Note: If you’re having trouble getting parameters in the query to work, after the first, try using & in place of & symbols, like post_type=book&author=hemingway

However, this will obviously not work:

array('post_type' => 'book', 'author' => 'hemingway')

So, as you explore WP_Query, you’ll naturally find that this really limits what you can do. You’re realistically limited to fairly simplistic queries.

Workaround for More Complex Queries

Now, if you’re trying to do more advanced queries, this is going to require actually setting up PHP arrays, and this won’t be possible through these basic custom query options which only accept a string, as discussed in the last section.

So, what if you need to do a more complicated query that requires PHP? — Internally, there is a filter on these query strings every time they’re used. But there’s no way to really identify this filter with a specific element’s instance. So as a workaround for this, in your instance of the element, you could pass a custom query string as some unique identifier to use as a “catch” within the filter you add.

For example, let’s say you set up some element, and for a custom query, you input the following.

my_unique_key

Now from your child theme’s functions.php, you can add a filter to themeblvd_posts_args, which will get attached to ALL instances of any elements pulling posts. So, then we’ll just check for our unqiue identifier we passed in, and return our custom query array.

function my_posts_args( $query, $args ) {

	if ( isset( $args['query'] ) && $args['query'] == 'my_unique_key' ) {
		$query = array( 
			// your custom query arguments
		);
	}

	return $query;
}
add_filter('themeblvd_posts_args', 'my_posts_args', 10, 2);