Open ardelmedia opened 4 years ago
Many people ask how to use the Elementor Custom Query Filter especially with custom fields.
You can take a look here to learn the basics: https://developers.elementor.com/custom-query-filter/
And you can see how a query looks here: https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/
What you need to know that you won't need to set an entire query inside the Custom Query Filter hook, you can alter it by changing/adding values.
For example we have this query (example from ACF):
$the_query = new WP_Query(array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'featured',
'orderby' => 'meta_value',
'order' => 'DESC'
));
If we want to change the meta_key value we just say in the hook:
$query->set( 'meta_key', 'start-date-for-course' );
And the new query would look like this:
array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'start-date-for-course',
'orderby' => 'meta_value',
'order' => 'DESC'
)
Hope you understand how it works.
So in the hook there is no need to add post_type and other values that you can set in the query section of elementor, just add the new ones.
In your case the hook should look like this:
// Adding a Hook to the Elementor Query Filter
add_action( 'elementor/query/my_custom_query', function( $query ) {
// ordered by meta_value
$query->set( 'orderby', 'meta_value' );
// Here we need to set the meta_key with meta_value = 'start-date-for-course'
$query->set( 'meta_key', 'start-date-for-course' );
} );
As you can see there is no need to add/change other values like post_type, order etc., everything else can be set in Elementor Editor.
Hopefully it makes sense.
if you use toolset you need to put wpcf- in front of the field name
Thank you so much for all of your help on this. You were incredible!
Yes, thanks, this is great. spent hours trying to work this out from elementor instructions. This worked straight using toolset custom fields.
Hi there.
I am trying to create a custom query in Elementor, but it's complex and you have very kindly offered to help.
Importantly this needs to be in a format so I can save the query and use it as a custom query in Elementor.
I have put all of the information below which I think you need, but please tell me anything I've missed.
Post type: courses Taxonomy: coursetypes Taxonomy term: one-day-courses Date field (which I need to query) by): start-date-for-course Order: Ascending order (i.e. next course first, later courses follow from there)
Thank you so much for your time. Andrew.