The native Query system build for Post widgets by Element Pack provides powerful solutions to sort out your posts to appear systematically according to your preferences. But still, you might want a Custom Query feature to customize the Query to apply your personalized filters.
To do that, Element Pack offers a Custom Query ID field within the Post widgets to give you the freedom of customization.
Let’s explore how you can take advantage of query customization.
1. Find Custom Query ID Field Inside Post Widgets
The native Query feature is available for all posts, portfolios, testimonials, and such post-type widgets. If you visit the Element Pack website and click on the Elements Demo menu, you will see a category filter named Post / Query inside the list, which sorts out all post-type widgets from Element Pack.
Now, open any page using the Elementor website editor and drag-drop a post widget (i.e. Post Slider by Element Pack) inside the page.
There, you can find the Custom Query ID field inside the Query section under the content tab.
Simply go to Content > Query > Query ID
You have to insert a query ID inside the field to make it functional over the post widget.
The Query path follows Element Pack’s native system: element_pack/query/my_query_id
here, my_query_id is a custom name for the query, and the name can be anything you want it to be.
2. Set Up Custom Query in WordPress
To setup a custom query in WordPress, you need to use a custom hook and a couple of lines of code.
Please follow these steps:-
- Go to WordPress Dashboard > Appearance > Theme File Editor > Open Functions.php file
- Scroll down and paste the custom hook code at the bottom.
add_action( 'element_pack/query/query_id', function( $query ) {
// Do what you want to do
});
Custom Query Examples
1. Using the hook, you can create custom queries to filter-out posts by post IDs like this-
// Exclude single posts by ID
add_action( 'element_pack/query/query_id', function( $query ) {
$query->set( 'post__not_in', array( 7, 11 ) );
});
2. You can create date filters to show posts only within a certain time frame-
// Get between post, 2015 - 2023
add_action( 'element_pack/query/query_id', function( $query ) {
$query->set('date_query', array(
array(
'after' => array(
'year' => 2015,
'month' => 01,
'day' => 01,
),
'before' => array(
'year' => 2023,
'month' => 01,
'day' => 01,
),
'inclusive' => true,
)
));
});
Following the examples, you can create many different custom queries to satisfy your needs.
Please find guidelines on Query customizations by WordPress Dev.