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

Check if current user has a post and that post has any terms from a specific custom taxonomy outside the loop

programmeradmin2浏览0评论

I'm looking for a way to check if (whilst outside the loop) the current user.. 1. is logged in 2. has a post published 3. their post has any term/terms from a custom taxonomy

I have this so far...

<?php if (( 1 == count_user_posts( get_current_user_id(), "post" ) && is_user_logged_in() ) { ?>
blah blah
<?php } ?>

I'm just wildly guessing here but could I use if( has_term( '', 'custom-taxonomy' ) )? but that's for use inside the loop.

I'm looking for a way to check if (whilst outside the loop) the current user.. 1. is logged in 2. has a post published 3. their post has any term/terms from a custom taxonomy

I have this so far...

<?php if (( 1 == count_user_posts( get_current_user_id(), "post" ) && is_user_logged_in() ) { ?>
blah blah
<?php } ?>

I'm just wildly guessing here but could I use if( has_term( '', 'custom-taxonomy' ) )? but that's for use inside the loop.

Share Improve this question edited Nov 23, 2019 at 2:25 Pete asked Nov 23, 2019 at 2:05 PetePete 1,0582 gold badges14 silver badges40 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This can be done using WP_Query using author and tax_query.

Something like this:

$args = array(
    'post_type' => 'post',
    'author' => get_current_user_id(),
    'tax_query' => array(
        array(
            'taxonomy' => 'custom-taxonomy',
            'operator' => 'EXISTS'
        ),
    ),
);
$query = new WP_Query( $args );

And then check if posts are returned through this query.

Please note that this code is not tried or tested and may contain syntax errors.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论