Support

Account

Home Forums ACF PRO Show only posts that have a relationship

Solved

Show only posts that have a relationship

  • I have some custom post types like Clients that I am relating to some other posts. What I want to do is display a list of my clients in a drop down menu. Thats easy enough to do with wp_query, but i need to filter out the clients that don’t have any relationships made.

    Is there a way to do that? To rephrase my question to make it clearer, I want to show only the clients that have been related to other posts. How to do that? Thanks!

    i should also point out that I am on a page not a post page so I don’t think i can do a reverse lookup. Not quite sure how to do this query.

  • You are looking for a not condition. Clients that have not been related to the other posts. The only way that I can think of to do this would be to

    1) do a query of all of the posts in the other post type
    2) Loop through all of the posts
    3) Create a list of unique client post IDs
    4) Use the list to query the client posts using posts__in

  • That would give me the posts that are related to “clients”, but thats not what i am trying to do.

    I’m trying to show the client names in a drop down menu if that client has been related to a post. Right now, i am just showing all of the client names, but then some of the client names don’t have anything related to them. So, when a user would select the client name from the drop down menu it won’t show any results. I want to filter those unrelated clients out from the list.

    Anyway to do that?

  • 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.
    }
    
  • great, thank you. I was able to get this to work.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Show only posts that have a relationship’ is closed to new replies.