WP_Query that filters based on custom relationship field

I have two custom post types: work and clients. On the work posts I set up a custom relationship field (client_name) where you can select one of the client posts.

On the work post I now want to check how many other work posts there are with the same client and get some other data from those posts. Here’s the code I have so far:

<?php
  $client = get_field('client_name');
  $work_args  = array(
    'post_type' => 'work',
    'meta_key' => 'client_name',
    'meta_value' => $client
  );
  $work_query = new WP_Query($work_args);
  if ($work_query->have_posts()):
  while ($work_query->have_posts()): $work_query->the_post();
?>
        /* Do something here */
<?php
  endwhile; endif;
  wp_reset_query();
?>

To be clear: the problem is not in the /* Do something here */ part, it’s in the meta_value. I can’t figure out what to put there so I can see if it matches the current client.

Any help is greatly appreciated, thanks!

1 Answer
1

well, I guess the problem is I’m trying to compare an array with a value. I made a workaround that gets me to where I need to go:

<?php
  $client = get_field('client_name');
  $work_args  = array(
    'post_type' => 'work'
  );
  $work_query = new WP_Query($work_args);
  if ($work_query->have_posts()):
  while ($work_query->have_posts()): $work_query->the_post();
?>
        $client2 = get_field('client_name');
    if ($client[0] == $client2[0] ){
         /*do something here*/
    }
<?php
  endwhile; endif;
  wp_reset_query();
?>

Leave a Comment