Support

Account

Home Forums ACF PRO How to combine all post types in a Relationship field

Solved

How to combine all post types in a Relationship field

  • I have a Relationship field that’s set to show two custom post types (e.g. Post Type 1 and Post Type 2). The posts are automatically shown under headers with the name of the post type. What I need to do is not show any headers and combine all of the posts from both Post Type 1 and Post Type 2 into a single listing sorted alphabetically by title. Is there a way to do this?

  • The posts may or may not be ordered in the same order that they are selected, there isn’t any way to tell. There isn’t a simple way to order posts by post type. You would need to modify the sql query that is being done by adding a filter before the query and then removing it when you’re done. Here is an example of altering results by post type. You’d need to modify it to also add ordering by title. https://stackoverflow.com/questions/15675296/wordpress-order-posts-by-post-type

  • Thanks John. This looks like it can help with sorting the results for each post type, but can this also help with combining the posts from each post type into a single listing?

    Basically the real use case is that I have two post types: Bio and Resume. The Relationship field needs to show both post types combined into a single listing, not separated by post type.

    Maybe it would be best to duplicate the Relationship field and just create a new field based on it.

  • In this case you need to have the posts in the relationship field associated with each other. You’d still have the same issue with two relationship fields.

    It might be better to use a repeater fields with two post object fields.

    However, the best solution that I can think of is created a relationship between “Bio” and “Resume” and then only selecting “Bio” in the relationship field. Then for each Bio get the related Resume.

  • Thanks for your help

  • Hey John, sorry for bringing this up again, but since I was having a hard time explaining the issue, I decided to just poke around until I figured it out but I’m still having problems.

    I think a simpler explanation is that the default way of displaying my posts in the relationship field is like this:

    Bios
    Jack Brown
    Jane Smith
    John Doe
    
    Resumes
    Jack Johnson
    James Thompson

    But I need the results to simply display like this:

    Jack Brown
    Jack Johnson
    Jane Smith
    James Thompson
    John Doe

    So no grouping by post type. I’ve poked around in the code and the get_ajax_query function calls the function acf_get_grouped_posts which groups the results by post type, and as far as I can see there’s no way to bypass the grouping function. So I’m thinking I need to create a custom ACF Relationship field/plugin that doesn’t call the acf_get_grouped_posts. I just want to check in real quick and see if I’m missing something before doing that, since it’s not an ideal solution.

    Thanks you!

  • What I ended up doing is extending “acf_field_relationship” with my own classname “acf_field_relationship_nogroup”. A few things needed to be customized, and I needed to register the new control in jQuery as well. It seems to be working well.

    Seems like not grouping the results by post type should be an option for Relationship fields though.

  • Had to make an update since the post IDs weren’t being included in the custom relationship field’s results correctly.

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

You must be logged in to reply to this topic.