[Resolved] Code snippet to show 'Locations the same as the page where this View is shown'
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
This is Elementor-related. I have a Toolset post type 'Destinations' with the taxonomies 'Destination types' and 'Locations'. In my Destinations post template, I have a Toolset View that shows related destinations from the same location, using the 'Locations the same as the page where this View is shown' filter.
I'm hoping to replace that View with an Elementor Loop Grid, which can accept custom PHP queries. Here's an example of one I use elsewhere, to display posts from multiple ACF post types:
Is it possible to create a similar snippet, but one which will display only destinations that have 'Locations the same as the page where this View is shown'?
To create a custom PHP query snippet for Elementor that displays only destinations with 'Locations the same as the page where this View is shown,' you can use the pre_get_posts filter to modify the main query. Here's an example snippet to achieve this:
function custom_elementor_query_destinations( $query ) {
// Check if we are in an Elementor query and on the Destinations post type.
if ( is_admin() || ! $query->is_main_query() || ! is_post_type_archive( 'destination' ) ) {
return;
}
// Get the current page's location
$current_location = get_the_terms( get_the_ID(), 'location' );
if ( ! empty( $current_location ) && ! is_wp_error( $current_location ) ) {
$location_ids = wp_list_pluck( $current_location, 'term_id' );
// Set the query to include only destinations with the same location.
$query->set( 'tax_query', array(
array(
'taxonomy' => 'location',
'field' => 'id',
'terms' => $location_ids,
'operator' => 'IN',
),
) );
}
}
add_action( 'pre_get_posts', 'custom_elementor_query_destinations' );
Please make sure to replace 'location' with the actual name of your location taxonomy and adjust the code according to your specific setup.
Here's how this snippet works:
1- It checks if we are in the main query and on the archive page of the 'destination' post type (adjust 'destination' to match your post type name).
2- It retrieves the current page's location (you may need to adjust this part based on how your location information is stored).
3- If a location is found, it sets the query to include only destinations with the same location using a tax_query.
Please note that the above code is custom and beyond the scope of our support. We've provided this as a courtesy to assist you further. If you require additional help or encounter issues, you might want to consider reaching out to Elementor support, consulting with a developer, or exploring qualified private contractors on our website at https://toolset.com/contractors/.
Make sure to insert this code into your theme's functions.php file. After adding the code, the Elementor Loop Grid widget should display only destinations with the same location as the current page.
If you have any more questions please feel free to ask.
Thank you for your reply. I do understand that this is beyond the scope of normal support. The code doesn't seem to be working for me, unfortunately. It doesn't have any effect on the query.
In checking my Tooslet configuration, I see that the slug for the Location taxonomy is 'dest-location', and the slug for the post type is 'destination'.
I've tried changing 'location' in your code sample to 'dest-location', but it doesn't make a difference. Can you think of anything I might be missing?
I would like to request temporary access (wp-admin and FTP) to your site to take better look at the issue. You will find the needed fields for this below the comment area when you log in to leave your next reply. The information you will enter is private which means only you and I can see and have access to it.
Our Debugging Procedures
I will be checking various settings in the backend to see if the issue can be resolved. Although I won't be making changes that affect the live site, it is still good practice to backup the site before providing us access. In the event that we do need to debug the site further, I will duplicate the site and work in a separate, local development environment to avoid affecting the live site.
- Please make a backup of site files and database before providing us access.
- If you do not see the wp-admin/FTP fields this means your post & website login details will be made PUBLIC. DO NOT post your website details unless you see the required wp-admin/FTP fields. If you do not, please ask me to enable the private box. The private box looks like this: hidden link
Please, let me know if you need any additional details. Have a nice day.
I was able to connect to the related posts in the same location using Elementor Loop Grid, please check: hidden link
I didn't have to use custom code, only using the built-in controls by Elementor and the info that's stored in the post by Toolset I was able to design it.
Can you please review it and let us know if this is what you were looking into achieving?
Thanks very much, Mateus. I could swear that was about the first thing I tried in the Loop Grid, but perhaps I missed a step. Anyway, yes, that replicates the way the previous View worked. Thanks again.