最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WP Query - Show custom posts only if user contain some user meta

programmeradmin3浏览0评论

I am trying to make a list of custom post type which going to show all posts but only if author contains custom user meta named "activeacc".

So.. I have thic code...

$args = array(
    'post_type' => 'property',
    'post_status' => 'publish'
);
$your_query = new WP_Query( $args );

while ( $your_query->have_posts() ) {
    $your_query->the_post();
    $the_title = get_the_title(); // variable $the_title now holds your title
}

Is it possible to add to this $args whether the author contains custom user meta 'activeacc'? If not, then do not show this posts (even they have "publish" status)?

I am trying to make a list of custom post type which going to show all posts but only if author contains custom user meta named "activeacc".

So.. I have thic code...

$args = array(
    'post_type' => 'property',
    'post_status' => 'publish'
);
$your_query = new WP_Query( $args );

while ( $your_query->have_posts() ) {
    $your_query->the_post();
    $the_title = get_the_title(); // variable $the_title now holds your title
}

Is it possible to add to this $args whether the author contains custom user meta 'activeacc'? If not, then do not show this posts (even they have "publish" status)?

Share Improve this question asked May 9, 2020 at 12:24 TereskaTereska 313 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You could approach in two steps. First get all the users that match your criteria

$user_ids = get_users([
    'meta_key' => 'activeacc',
    'meta_value' => true, // or whatever value you are storing
    'fields' => 'ID',
]);

and then run your WP_Query for only those users

$properties = new WP_Query([
    'post_type' => 'property',
    'author__in' => $user_ids,
]);

There is no need to set post_status => 'publish' since it's the default.

Also would be possible to accomplish the same in just one query with posts_clauses filter but I will not develop it here.

See https://developer.wordpress/reference/classes/wp_user_query/prepare_query/ for all available arguments for get_users.

发布评论

评论列表(0)

  1. 暂无评论