Support

Account

Home Forums ACF PRO Show only posts that have a relationship Reply To: Show only posts that have a relationship

  • There really isn’t any way in WP to do a negative query especially a reverse negative query. You want to remove related posts that are not related.

    For that you need to get a list of all of the posts that have relationships and only show those.

    While WP_Query is a powerful tool, it has certain limitations.

    In order to filter out the clients that are not related from your drop down you’re going to have to create a list of all of the clients that do have relationships and then only show the ones that do. Using WP_Query the only way that I can think of to do this is like I said above.

    – Do a query of all the posts that clients can be related to.
    – Loop through these results and build a list of clents that are related

    
    // please not that this code is just an outline as an example
    $related_list = array();
    while (have_posts()) {
      the_post();
      $related_clients = get_field('related_clients');
      // I'm assuming that the above returns an array of ID values
      foreach ($related_clients as $client_id) {
        if (!in_array($client_id, $related_list)) {
          $related_list[] = $client_id;
        }
      }
    }
    

    Now that we have a list of all the clients that have relationships

    – Do a query to get all the clients and we do not show the ones that don’t have a relationship

    
    while (have_posts()) {
      the_post();
      if (!in_array($post->ID, $related_list)) {
        continue;
      }
      // code to display that client in the drop down here.
    }