Title: [Plugin: Developer&#039;s Custom Fields] Query Posts by User Custom Fields
Last modified: August 20, 2016

---

# [Plugin: Developer's Custom Fields] Query Posts by User Custom Fields

 *  [raonip](https://wordpress.org/support/users/raonip/)
 * (@raonip)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/)
 * Hi, please, I need some help.
 * I Have a user checkboxes custom field registered to use a Taxomony and is set
   to $Single= false. So I can check as many as I want.
 * I use this taxonomy also in my custom post type where my client can check one
   or many tags for each post.
 * So, what I need is to query all posts that has the same tags for each user. I
   have the code below:
 *     ```
       if ( function_exists( 'slt_cf_register_box') )
           add_action( 'init', 'register_my_custom_fields' );
   
       function register_my_custom_fields() {
           slt_cf_register_box( array(
               'type'      => 'post',
               'title'     => 'Dados do Arquivo',
               'id'        => 'dados-arquivo',
               'context'   => 'normal',
               'priority'  => 'high',
               'fields'    => array(
                   array(
                       'name'          => 'ad_arquivorestrito',
                       'label'         => 'Selecione o Arquivo',
                       'type'          => 'file',
                       'file_button_label'          => 'Selecionar arquivo',
                       'scope'         => array( 'ad_arearestrita' ),
                       'capabilities'  => array( 'edit_posts' )
                   )
               )
           ));
           slt_cf_register_box( array(
               'type'      => 'user',
               'title'     => 'Acesso as Concessionárias',
               'id'        => 'acesso-concessionaria',
               'context'   => 'normal',
               'priority'  => 'high',
               'fields'    => array(
                   array(
                       'name'          => 'ad_acessoconcessionaria',
                       'label'         => 'Acesso a:',
                       'type'          => 'checkboxes',
                       'multiple'      => true,
                       'options_type'  => 'terms',
                       'options_query' => array( 'taxonomies' => 'ad_concessionaria', 'hide_empty' => false ),
                       'single' 		=> false,
                       'scope'         => array( 'administrator', 'admin', 'concessionaria', 'editor' ),
                       'capabilities'  => array( 'edit_users' )
                   )
               )
           ));
       }
       ```
   
 * That’s working fine! I have a checkboxes in my User Profile with all tags I created
   in the Taxonomy “ad_concessionaria”.
 * Now I have the query below:
 *     ```
       <?php
       $args = array(
       	'post_type' => 'ad_arearestrita',
       	'posts_per_page' => -1,
       	'tax_query' => array(
       		array(
       			'taxonomy' => 'ad_concessionaria',
       			'field' => 'id',
       			'terms' => $current_user->_slt_ad_acessoconcessionaria
       		)
       	)
       );
       $query = new WP_Query( $args );
       while($query->have_posts()) : $query->the_post();
       ?>
       ```
   
 * That’s working fine, but is retrieving just one post, because is getting just
   ONE “term” from my User Custom Field…….
 * I can’t find a way to retrieve an Array of the values, since I use Checkboxes
   and I can have many tags in this field…..
 * Can you help me please??? How can I get an ARRAY of values to use in this Query?
 * [http://wordpress.org/extend/plugins/developers-custom-fields/](http://wordpress.org/extend/plugins/developers-custom-fields/)

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Thread Starter [raonip](https://wordpress.org/support/users/raonip/)
 * (@raonip)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/#post-2543696)
 * Hi, it’s me again! What I need is to have an Array like this:
 *     ```
       array(
       			'taxonomy' => 'ad_concessionaria',
       			'field' => 'id',
       			'terms' => array( 103, 115, 206 )
       		)
       ```
   
 * How can I pass my User Custom field as an ARRAY inside the ‘terms’??
    I forgot
   to say the USER ID is the user logged in!
 * Thanks and I hope someone can help!!!
 *  Plugin Author [Steve Taylor](https://wordpress.org/support/users/gyrus/)
 * (@gyrus)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/#post-2543697)
 * Just trying to get my head around the issue:
    1. You have a taxonomy applied to a custom post type (using the WP taxonomy system,
       not a custom field)
    2. You have the same taxonomy applied to users, via a custom field
    3. You want the query to return all posts that have at least one tag that is also
       attached to a particular user? Or…?
 * I’m not clear at all about #3! Could you clarify?
 *  Plugin Author [Steve Taylor](https://wordpress.org/support/users/gyrus/)
 * (@gyrus)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/#post-2543699)
 * Ah, OK. Well, the Codex says that the `terms` in the above array can be an array.
   Maybe you need to play around with the other parameters – maybe `operator`?
 *  Thread Starter [raonip](https://wordpress.org/support/users/raonip/)
 * (@raonip)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/#post-2543700)
 * Hi, I think I just need to retrieve the values of the user custom field as an
   Array in ‘Terms’……
 *  Thread Starter [raonip](https://wordpress.org/support/users/raonip/)
 * (@raonip)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/#post-2543701)
 * The way I did in my query I can have just ONE value…….and as a result I have 
   just One post with that same value (tag).
 * If I can pass an ARRAY with all values………then I have all posts….
 *  Thread Starter [raonip](https://wordpress.org/support/users/raonip/)
 * (@raonip)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/#post-2543703)
 * Hi, I tried this query but is not working:
 *     ```
       <?php
       $args = array(
       	'post_type' => 'ad_arearestrita',
       	'posts_per_page' => -1,
       	'tax_query' => array(
       		array(
       			'taxonomy' => 'ad_concessionaria',
       			'field' => 'id',
       			'terms' => slt_cf_field_value( 'ad_acessoconcessionaria' )
       		)
       	)
       );
       $query = new WP_Query( $args );
       while($query->have_posts()) : $query->the_post();
       ?>
       ```
   
 *  Plugin Author [Steve Taylor](https://wordpress.org/support/users/gyrus/)
 * (@gyrus)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/#post-2543704)
 * Maybe:
 * `'terms' => slt_cf_field_value( 'ad_acessoconcessionaria', 'user', $user_id, '','',
   false, false )`
 * The `false` at the end is to reflect `single` being false in the field definition:
 * [http://sltaylor.co.uk/wordpress/plugins/slt-custom-fields/docs/#functions-values](http://sltaylor.co.uk/wordpress/plugins/slt-custom-fields/docs/#functions-values)
 * You’ll have to replace `$user_id` with the current user ID, however you’re getting
   that.
 *  Thread Starter [raonip](https://wordpress.org/support/users/raonip/)
 * (@raonip)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/#post-2543707)
 * Steve, YOU ARE THE MAN!!! THANK YOU SO MUCH!!
    Simple as that!! 🙂
 * Now it’s working….here is the query PERFECT:
 *     ```
       <?php
       $args = array(
       	'post_type' => 'ad_arearestrita',
       	'posts_per_page' => -1,
       	'tax_query' => array(
       		array(
       			'taxonomy' => 'ad_concessionaria',
       			'field' => 'id',
       			'terms' => slt_cf_field_value( 'ad_acessoconcessionaria', 'user', $current_user->ID, '', '', false, false )
       		)
       	)
       );
       $query = new WP_Query( $args );
       while($query->have_posts()) : $query->the_post();
       ?>
       ```
   
 * Thank you so much!!! Finally I found an excelent Custom Fileds Plugin!
 *  Thread Starter [raonip](https://wordpress.org/support/users/raonip/)
 * (@raonip)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/#post-2543710)
 * Steve, where can I donate some money for you?? To continue the development of
   this plugin??
 * Thanks again!
 *  Plugin Author [Steve Taylor](https://wordpress.org/support/users/gyrus/)
 * (@gyrus)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/#post-2543717)
 * Fantastic, glad it worked and the plugin’s proving useful!
 * There’s a donate link on the plugin home page:
 * [http://wordpress.org/extend/plugins/developers-custom-fields/](http://wordpress.org/extend/plugins/developers-custom-fields/)
 * Thanks 🙂
 *  Thread Starter [raonip](https://wordpress.org/support/users/raonip/)
 * (@raonip)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/#post-2543720)
 * Perfect!! Just Donated!!!!

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘[Plugin: Developer's Custom Fields] Query Posts by User Custom Fields’
is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/developers-custom-fields.svg)
 * [Developer's Custom Fields](https://wordpress.org/plugins/developers-custom-fields/)
 * [Support Threads](https://wordpress.org/support/plugin/developers-custom-fields/)
 * [Active Topics](https://wordpress.org/support/plugin/developers-custom-fields/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/developers-custom-fields/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/developers-custom-fields/reviews/)

 * 11 replies
 * 2 participants
 * Last reply from: [raonip](https://wordpress.org/support/users/raonip/)
 * Last activity: [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-developers-custom-fields-query-posts-by-user-custom-fields/#post-2543720)
 * Status: not resolved