Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Supposing I have this function which I hooked into my_get_users function:

function define_users($group) {

//Query database for users belonging to a group
global $wpdb;

$users= $wpdb->get_results("SELECT user_id from $wpdb->usermeta WHERE meta_key='groupname' AND meta_value=$group",ARRAY_N);

return $users;

}

add_filter('my_get_users','define_users',10,1);

This is my_get_users function where the apply_filters are found:

function my_get_users($group) {

//Define users as array
$users=array();     


$users = apply_filters('my_get_users', $users, $group);


    return $users;

}

I am sure there is data in the database but the $users variable is returning as an empty array. Any ideas what's wrong? Thanks for your help.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

try this

$users = apply_filters('my_get_users',$group);

share|improve this answer
    
Thanks, it's now working.. –  Codex Meridian Feb 26 '13 at 11:19
2  
@Jesse nice answer, but please try to explain what went wrong to help answer the question. –  fischi Feb 26 '13 at 11:26

You have a few mistakes here, but your general direction is the right one.

Your line

apply_filters('my_get_users', $users, $group);

Implies that 2 Parameters are passed on to your function my_get_users. You pass $users as an empty array, which is not necessary, but your main problem here is another one.

You hook into your filter by

add_filter('my_get_users','define_users',10,1);

This line tells WordPress that your function define_users accepts 1 parameter (the value of the 4th parameter in this line).

So WordPress tries to pass 2 Parameters:

  • $users (an empty array)
  • $group

As your function is set to only accept one paremter, it just gets the empty $users array.

In your function define_users($group) $group gets the value of your previous $users, and so your Query will not return a correct result, as you use $group in there.

The solutions would be (two possibilities):

Pass just one Parameter to your Filter

function define_users($group) {

    //Query database for users belonging to a group
    global $wpdb;

    $users= $wpdb->get_results("SELECT user_id from $wpdb->usermeta WHERE meta_key='groupname' AND meta_value=$group",ARRAY_N);

    return $users;

}

add_filter('my_get_users','define_users',10,1);

function my_get_users($group) {

    $users = apply_filters('my_get_users', $group);

    return $users;

}

Define your function for 2 Parameters (not used in this case)

function define_users($users, $group) {

    //Query database for users belonging to a group
    global $wpdb;

    $users= $wpdb->get_results("SELECT user_id from $wpdb->usermeta WHERE meta_key='groupname' AND meta_value=$group",ARRAY_N);

    return $users;

}

add_filter('my_get_users','define_users',10,2);

function my_get_users($group) {

    //Define users as array
    $users=array();     

    $users = apply_filters('my_get_users', $users, $group);

    return $users;

}

What else to say

  • Please check the following things: Do you really need to place a manual query or could this be done by get_users?
  • In this case there is actually no need for a filter, unless you have other functions with a hook for altering your userlist.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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