WordPress.org

Forums

Developer's Custom Fields
Query Posts by User Custom Fields (12 posts)

  1. raonip
    Member
    Posted 3 years ago #

    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/

  2. raonip
    Member
    Posted 3 years ago #

    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!!!

  3. Steve Taylor
    Member
    Plugin Author

    Posted 3 years ago #

    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?

  4. Steve Taylor
    Member
    Plugin Author

    Posted 3 years ago #

    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?

  5. raonip
    Member
    Posted 3 years ago #

    Hi, I think I just need to retrieve the values of the user custom field as an Array in 'Terms'......

  6. raonip
    Member
    Posted 3 years ago #

    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....

  7. raonip
    Member
    Posted 3 years ago #

    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();
    ?>
  8. Steve Taylor
    Member
    Plugin Author

    Posted 3 years ago #

    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

    You'll have to replace $user_id with the current user ID, however you're getting that.

  9. raonip
    Member
    Posted 3 years ago #

    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!

  10. raonip
    Member
    Posted 3 years ago #

    Steve, where can I donate some money for you?? To continue the development of this plugin??

    Thanks again!

  11. Steve Taylor
    Member
    Plugin Author

    Posted 3 years ago #

    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/

    Thanks :)

  12. raonip
    Member
    Posted 3 years ago #

    Perfect!! Just Donated!!!!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Developer's Custom Fields
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic