0

I'm using a plugin (User Groups) to separate out users into, well, groups, and I need to figure out a way to create a conditional to check if an author is in a certain group. So, using get_term_by and get_objects_in_term, I was able to build a string of author IDs. See below:

$taxonomy = 'user-group';
$taxonomy_group_name = 'author-group';
$taxonomy_group_id = get_term_by('slug' , $taxonomy_group_name, $taxonomy, 'OBJECT');
$taxonomy_group_ids = get_objects_in_term($taxonomy_group_id->term_id, $taxonomy);
$taxonomy_id_list = implode(',', $taxonomy_group_ids);

So, I can take the list stored in $taxonomy_id_list and echo it out and get a string that looks like the below:

1,2,3,4,5,6,7

For the conditional, I should then be able to say:

if(is_author(array($taxonomy_id_list))) { }

But that doesn't work. If I paste the raw list into the array, it works as it should, but if I try to use the variable inside the array, the conditional does not work.

What am I missing? Why wouldn't that variable work?

1

You've converted $taxonomy_id_list to a comma separated string then shoved that string into an array. Your array now looks like

array(
  "1,2,3,4"
);

That is not going to match any author ID.

You are misunderstanding what implode does. This--1,2,3,4,5,6,7-- is a set of integers. If place in the array like this-- array(1,2,3,4,5,6,7)-- you get an array with 7 arguments. If you implode the IDs and put that into the array you have this-- array("1,2,3,4,5,6,7")-- an array with one argument.

Just skip this step: $taxonomy_id_list = implode(',', $taxonomy_group_ids); $taxonomy_group_ids, if it implodes the way you say, is already the array you need. There is no need to implode it only to try to reconvert it to an array.

  • I get what you're saying about the implode, and that makes sense. What I'm not sure about is how I can skip that step. $taxonomy_id_list doesn't exist until I define it with the implode function, and if I use the variable a step before, $taxonomy_group_ids, I get an error. – Charles Jul 19 '13 at 22:31
  • But you have $taxonomy_group_ids. Sorry. Didn't notice the variable change. – s_ha_dum Jul 19 '13 at 22:33
  • Brilliant, got it. 100% makes sense now. is_author($taxonomy_group_ids) works for the array with one less step. Thanks so much. – Charles Jul 19 '13 at 22:36

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.