WP Code Labs
Search…
Filters
WP Query Engine provides several filters that allow you to customize many aspects of operation.

wp_query_engine_templates

Output templates can be added, removed, or modified using the wp_query_engine_templates filter.
Template registration is not required for use in a shortcode, but is required to add custom templates to select lists, such as in the Beaver Builder module or sidebar widget.
1
function register_templates( $templates ) {
2
$templates['My Custom Template'] = get_stylesheet_directory() . '/templates/my_custom_template.php';
3
return $templates;
4
}
5
add_filter( 'wp_query_engine_templates', 'register_templates' );
Copied!

wp_query_engine_template

Force the use of a specific template, regardless of which template is selected. The wp_query_engine_template filter allows developers to select a template programmatically at the time of output, instead of specifying beforehand.
1
function force_template( $template_name, $atts, $query ) {
2
if( $atts['context'] === 'my_custom_context' ) {
3
$template_name = 'templates/my_custom_template.php';
4
}
5
return $template_name;
6
}
7
add_filter( 'wp_query_engine_template', 'force_template', 10, 3 );
Copied!

wp_query_engine_args_raw

Filter the query arguments before they are processed. This allows you to add additional arguments, or control the arguments within your code, instead of the shortcode or other means.
1
function force_query_args( $args ) {
2
if( isset( $args['post_type'] ) && $args['post_type'] === 'recipe' ) {
3
$args['tag__in'] = array( 'featured' );
4
}
5
return $args;
6
}
7
add_filter( 'wp_query_engine_args_raw', 'force_query_args' );
Copied!

wp_query_engine_args

Filter the query arguments after processing. This allows developers to apply some specific formatting to the arguments after they've been normalized, but before being passed to WP_QUERY. At this point, the post id's that will be queried is already set, so fewer arguments can be applied.
1
function force_query_args( $args ) {
2
if( isset( $args['post_type'] ) && $args['post_type'] === 'recipe' ) {
3
$args['post_per_page'] = -1;
4
}
5
return $args;
6
}
7
add_filter( 'wp_query_engine_args', 'force_query_args' );
Copied!

wp_query_include_loop

By default, the template includes a loop with specific template actions already defined. The loop uses the template name to define custom actions.
The template name is normalized by removing spaces, dashes, and .php. If a template is defined by name, such as My Custom Template, it will be normalized to my_custom_template. A name specified by path, such as templates/homepage_loop.php, it will be normalized to templates_homepage_loop.
If you do not wish to include the default loop, and instead want to define your own loop in your custom template files, you can using the wp_query_include_loop filter.
1
function dont_include_wp_query_loop( $include, $atts ) {
2
if( $atts['template'] === 'My Custom Template' ) {
3
return false;
4
}
5
return true;
6
}
7
add_action( 'wp_query_include_loop', 'dont_include_wp_query_loop' );
Copied!
The loop is includes after the template files, however. So it can be defined inside specific templates, with a simpler function
1
add_action( 'wp_query_include_loop', '__return_false' );
Copied!
Be careful, however, not to define this globally in your functions.php file unless an alternative loop is provided for the default templates, or they will not output.
Last modified 2yr ago