Reputation: 41
I am creating a custom archive page with Elementor Theme Builder which will be applied to all category pages. The traditional 'Archive' widget shows all the posts and the post widget requires a term.
How can I get the posts to display dynamically on the respective archive page using Elementor Custom Query Filter? https://developers.elementor.com/custom-query-filter/
add_action( 'elementor/query/my_custom_filter', function( $query ) {
// Get current meta Query
$meta_query = $query->get( 'meta_query' );
// If there is no meta query when this filter runs, it should be initialized as an empty array.
if ( ! $meta_query ) {
$meta_query = [];
}
// Append our meta query
$meta_query[] = [
'key' => 'category',
'value' => get_the_ID(),
'compare' => '=',
];
$query->set( 'meta_query', $meta_query );
} );
Upvotes: 3
Views: 5363
Reputation: 196
I'm posting this for other users coming here from the top search of google:
You can totally do this, but you need to pass the post parameters from the query function:
function custom_query_callback( $query ) {
// grab curent post data
global $post;
$id = $post->ID;
// grab curent post category data
$cat_id = get_the_category($id);
$cat = $cat_id[0]->cat_ID;;
// add category filter to elm query
$query->set( 'cat', $cat);
}
add_action( 'elementor/query/custom_related_posts', 'custom_query_callback' );
additionally you can add this to link a button to the archive page:
function category_link_shortcode(){
// grab current post data
global $post;
$id = $post->ID;
// grab current post category data
$cat_id = get_the_category($id);
$cat = $cat_id[0]->cat_ID;;
// grab current post category link
$link = get_category_link($cat);
// return for elementor button link dynamic tag
return $link;
}
add_shortcode('category_link_shortcode', 'category_link_shortcode');
you can use these on loop grid query as well as standard posts widget query
Upvotes: 1
Reputation: 160
You could use the archive posts widget instead, with the query set to current query, so it automatically detects the archive category.
Upvotes: 2