0

I'm trying to pass ACF field value in query as value. Let me show what I have now:

array(                              
    'key' => 'office', // name of custom field
    'value' => '320', // matches exaclty "123", not just 123. This prevents a match for "1234"
    'compare' => 'LIKE'
),

As you see, I have static value now, this is an ID of post object. The problem is that this is not always "320", so I've created another relationship to choose which office you want. But now I'm confused how to pass that field value into query. I tried something like this but it return all users from all offices

$valuee = get_field( "choose_office" );
$args = array(
    'meta_query' => array(
        array(
            'key' => 'office', // name of custom field
            'value' => $valuee, // matches exaclty "123", not just 123. This prevents a match for "1234"
            'compare' => 'LIKE'
        ),
    ),  
);
5
  • Is your variable really called $valuee or is it a typo? And why do you use LIKE as compare method, if you want only exact matches? May 23, 2018 at 5:48
  • it is really valuee, well i used reverse query approach from acf documentation they use like, so i used also and there is no problem for now, i get correct results until now. advancedcustomfields.com/resources/querying-relationship-fields -revese query approach May 23, 2018 at 9:21
  • And what is the type of this field? Is it relationship field? In such case get_field will return an array, so your query won't work... May 23, 2018 at 22:13
  • Yes it is relationship field, ok, any sugestion about this? May 24, 2018 at 17:21
  • So i find answer in another question on stack: stackoverflow.com/questions/46816190/… May 24, 2018 at 17:41

1 Answer 1

0

SOLUTION:

$posts = get_field('choose_office'); if( $posts ): 
 foreach(array_slice($posts, 0, 1) as $post):
  setup_postdata($post);
  $post_ID =  get_the_ID();
   endforeach; 
   wp_reset_postdata();
   endif;

  $args = array(


'meta_query' => array(

        array(

'key' => 'office', // name of custom field-post object
'value' => $post_ID, //name of relationship field
'compare' => 'LIKE'
                            ),

                        ),  
    );

Solution from question: https://stackoverflow.com/questions/46816190/using-post-id-inside-reverse-acf-relationship-querie

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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