John Davidson

php - Elementor Posts Widget Custom Query: found_posts return 0

0 comments
Message:


I am creating a custom query based on some selected posts in Elementor pro Posts Widget. below is code. The query is displaying posts correctly in Posts widget but found_posts always shows zero value. Can some body help me.


 function my_fav_by_user($fav_query) {

$fav_query->set( 'post__in', array(2783,2780,2793,2528,2873, 2477,2892,2890));

_e("Total Posts=".$fav_query->found_posts, 'my-text-domain');

}
add_action( 'elementor/query/my_fav_query3', 'my_fav_by_user' );


My Answer:
>set('post__in', array(1, 2, 3)); // Replace 1, 2, 3 with the IDs of the posts you want to display__ return $fav_query;__}__add_filter('elementor/query/{$query_id}', 'my_fav_by_user');__?>

In the above code, you are using a custom query to display specific posts in the Elementor Posts Widget. However, the `found_posts` value is always returning 0 because Elementor does not update this value when using a custom query.

To work around this issue, you can manually set the `found_posts` value in the `elementor/query/{$query_id}` filter function. Here's an updated version of your code:

php
function my_fav_by_user($query) {
$query->set('post__in', array(1, 2, 3)); // Replace 1, 2, 3 with the IDs of the posts you want to display

// Manually set the found_posts value
$query->found_posts = count($query->get('post__in'));

return $query;
}

add_filter('elementor/query/{$query_id}', 'my_fav_by_user');


In this updated code, we are manually setting the `found_posts` value to the count of the posts in the `post__in` array. This should display the correct number of posts in the Elementor Posts Widget.

Rate this post

4 of 5 based on 6348 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us