0

I'm trying to set up something to decide wether the current post has a custom taxonomy term that matches the current users ID.

So far I set up a variable that stores the taxonomy terms of the given post with

$terms = get_terms( array(
    'taxonomy' => 'list'
));

But now I don't know how to loop through that list of terms in order to match one of the terms names with the user ID. Also, is there a way to do this without running into troubles when for example the user ID is 3 and the post has a term with the name of 30. In this case I do not want this to be a match.

Any guidance is appreciated.

  • Your question is not clear enough are you matching term_id = user_id or you have some other ideas? – Adeel Abbas Jun 26 '18 at 14:08
  • No, the name of the Term should correspond with the user id. So all the term names consist of numbers only. – Philipp K Jun 26 '18 at 14:12
  • Do you have the array of users or you have particular id to match? – Adeel Abbas Jun 26 '18 at 14:22
  • I want to check if there is a match for the current users ID – Philipp K Jun 26 '18 at 14:26
0

Try this code it will surely work, before testing this code make sure you have added the categories title as numbers.

$terms = get_terms( array(
    'taxonomy' => 'list',
    'hide_empty' => false
));
$user = wp_get_current_user();
if(!empty($terms)){
    foreach ( $terms as $term ) {
        if($term->name == $user->ID){
            // Do something
        }
    }
}

This code will works only for post which id you gave.

$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
// You need to add Current Post ID here.
$get_terms = wp_get_post_terms($post_id,'list',$args);

//Get Current User Info
$user = wp_get_current_user();

//Check array is not empty
if(!empty($get_terms)){
    foreach ( $get_terms as $term ) {
        if($term->name == $user->ID){
            // Do something
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.