Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • wp_list_filter wordpress function

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 329
    Comment on it

    Welcome to Findnerd.

    In a series of word-press functions, today I am going to discuss the word-press function wp_list_filter. It's working as per the name. If you want to filter an array or a list then it can be a beneficial one. This function works with wp version 3.1 or above. First of all, we will discuss its argument and then will take examples.

     

    Parameters Properties
    list(array) It is a required argument. This is a list to filter.
    args(array) It is optional one and the default value is an empty array. The list will be filtered as per this array.
    Operator(string) It contains three different logical operators. 'AND' set the rule to match all the elements from the array. 'OR' set the rule to match the only one element from the array. and last is 'NOT' means not element may match. The default is set to 'AND'.

     

    In above table, I have described the properties of parameters. We start with a small example

    --------------- example1 ---------------------
    $list = array('Amit','siddharatha','shyam','ram','negi','Rajesh');
    $args = array('shyam'); 
    $filtered_one = wp_list_filter($list,$args,'AND');
    print_r($filtered_one);
    
    //result: 
    Array ( [2] => shyam ) 
    
    --------------- example2 ---------------------
    $list = array('Amit','siddharatha','shyam','ram','negi','Rajesh');
    $args = array('shyam','ram'); 
    $filtered_one = wp_list_filter($list,$args,'AND');
    print_r($filtered_one);
    
    //result
    array()

     

    It is working with a difference as it mentioned in the documentation. If you check the first example then you can see two arrays with different names. the second array contains only one element. Here I am using operator 'AND' which implies the matching for all element. In the first example, it will return an array with the same index as the element has in the first array but in the second example, it will return an empty array. I am using two elements from the first array but it is working only with one matched element in the second array.

    Now trying with operator 'OR'. Please have a look.

    --------------example1-----------------
    $list = array('amit','siddharatha','shyam','ram','negi','Rajesh');
    $args = array('amit','siddharatha'); 
    $filtered_one = wp_list_filter($list,$args,'OR');
    print_r($filtered_one);  
    //result
    Array ( [0] => amit ) 
    -----------------example2--------------------
    $list = array('amit','siddharatha','shyam','ram','negi','Rajesh');
    $args = array('amit'); 
    $filtered_one = wp_list_filter($list,$args,'OR');
    print_r($filtered_one);
    
    //result
    Array ( [0] => amit )

    In both the example, it is returning the same result with the index value. It means operators will work only one element from the second array as you can see in above examples.

    Last trying with operator 'NOT'. Please have a look.

    --------------example1-----------------
    $list = array('amit','siddharatha','shyam','ram','negi','Rajesh');
    $args = array('amit','siddharatha'); 
    $filtered_one = wp_list_filter($list,$args,'NOT');
    print_r($filtered_one);  
    //result
    Array ( [1] => siddharatha [2] => shyam [3] => ram [4] => negi [5] => Rajesh ) 
    -----------------example2--------------------
    $list = array('amit','siddharatha','shyam','ram','negi','Rajesh');
    $args = array('amit'); 
    $filtered_one = wp_list_filter($list,$args,'NOT');
    print_r($filtered_one);
    
    //result
    Array ( [1] => siddharatha [2] => shyam [3] => ram [4] => negi [5] => Rajesh ) 

    In both examples, it eliminates the first element of the second array in filtered array. It is now clear that this function can not handle much filtering process so it is not so beneficial.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: