Query filter on custom post by current author in post widget for elementor query id

admin2025-01-08  4

I am using post widget in elementor pro. Idea is to show all posts by a specific custom post type. Now, I want to apply also some filters in particular I want to show only posts in a particular status (ie, "draft") and those created ("post author") by the user logged in.

I have been able to create a query for status and pass the query id (my_query_by_post_status), this is the case:

add_action( 'elementor/query/my_query_by_post_status', function( $query ) {
$query->set( 'post_status', ['draft'] );
}   );

But I don't know how to add a condition to say "only for author same as the current user logged in".

Can anybody help me in this? I really appreciate and thank you in advance. Mario.

I am using post widget in elementor pro. Idea is to show all posts by a specific custom post type. Now, I want to apply also some filters in particular I want to show only posts in a particular status (ie, "draft") and those created ("post author") by the user logged in.

I have been able to create a query for status and pass the query id (my_query_by_post_status), this is the case:

add_action( 'elementor/query/my_query_by_post_status', function( $query ) {
$query->set( 'post_status', ['draft'] );
}   );

But I don't know how to add a condition to say "only for author same as the current user logged in".

Can anybody help me in this? I really appreciate and thank you in advance. Mario.

Share Improve this question asked Oct 9, 2022 at 20:06 IoriaIoria 293 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

get current user id using get_current_user_id() (will return 0 if no user logged in)

$current_user = get_current_user_id();

if($current_user != 0){
  $query->set( 'author', [$current_user] );
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265764a1067.html

最新回复(0)