How to add Custom Query to Posts Widget in Elementor

Last Updated: March 8, 2021 | Reading Time: < 1 minute

Elementor has provided a good widget for you to display content from the regular posts or other custom post types. But sometimes, the default options just not enough, the post widget didn’t provide you a complete option.

Luckily, Elementor has provided “query id” for this purpose, it works exactly like the “pre_get_post”. Here’s how:

Add Post Element

After add the element, go to query tab and add query id name.

Query ID

on the other hand to control the query, add this code to the functions.php:

add_action( 'elementor/query/customquery', function( $query ) {
 // Set the custom post type 
$query->set( 'post_type', [ 'flora', 'fauna' ] ); 

// Set for the posts_per_page 
$query->set( 'posts_per_page', 12 ); 

// Set for the custom field 
$meta_query[] = [          
'key' => 'kebutuhan_cahaya',          
'value' => [ 'Sedang' ],          
'compare' => 'in', ];  

$query->set( 'meta_query', $meta_query );

 } );

You can refer to the WP_Query documentation for complete parameters.

2 Responses

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.