tvsupp
tvsupp

Reputation: 11

Query in Elementor Pro to display posts by Custom Taxonomy

My issue is related to WordPress / Elementor, but I need help in fixing (modifying) a query. Not sure if this is the right place to post this.

For an Elemnetor Pro based web project, I have set up a custom template that displays some Custom fields coming from the ACF and Custom Post Type UI. The page also includes a Post Widget which is supposed to display the latest posts that are tagged with a Custom Taxonomy that was created in CPT and successfully integrated through ACF and tagged for each Custom Post.

I found a solution that almost does what I aim to achieve by adding a query by creating a snippet and applying a query ID to the Widget in Elementor settings.

The issue is that it displays the related post by WordPress tags smoothly, but I need it to display by Custom Taxonomy.

Following is the query that was taken from https://www.metronovacreative.com/website-design/match-current-pages-terms-elementor-custom-queries/

This works great for the WP tags. Can someone help me edit this query to get the custom taxonomy instead of the tags?

Not a developer, so absolutely have no idea what should be done. Your help is highly appreciated.

add_action( 'elementor/query/limitpost', function( $query ) {
    $tag_objects = get_the_tags();
foreach( $tag_objects as $temp_tag) {
$tag_slugs\[\] = $temp_tag-\>slug;
}

    $query->set( 'tag_slug__in', $tag_slugs );
    $query->set( 'post_type', 'post' );
    $query->set( 'post_status', 'publish');
    $query->set( 'order', 'DESC');
    $query->set( 'orderby', 'date' );

} );

Upvotes: 1

Views: 1426

Answers (1)

Doesn't need you anymore, but for all new searchers:

If you have some ACF posts types and want to create custom query for some/all of them, but also to use custom category/tag ets. You don't need to make this through code completely.

You just need to change source of posts:

function my_query_by_post_types( $query ) {
    $query->set( 'post_type', [ 'custom-post-type1', 'custom-post-type2' ] );
}
add_action( 'elementor/query/{$query_id}', 'my_query_by_post_types' );

All other configuration you can make inside Elementor page builder inside Loop Grid widgets for example.

You just need to select any ACF post type as source and place query_id into Query ID field.

Everything alse you can do with widget UI. I mean category/tag filter, changing order, place some exclude rules. All this will automatically apply to your query.

Hope it will help someone!

Upvotes: 0

Related Questions