Home › Forums › Backend Issues (wp-admin) › Hierarchical Custom Posts using Relationship custom field
Hi
I have a task of relating two Custom Post Types together using the Relationship field.
The first post type is called ‘Product Categories’. This is a hierarchical set of categories which hold a title and some descriptive text. For example:
1/ Electrical Items
1.1/Drills
1.2/Mowers
2/ Timber
3/ Building Materials
3.1/ Bricks
3.2/ Plumbimg
The second post type is called ‘Branches’. This holds information about a branch such as address, telephone numbers etc etc. It also has a relationship field which allows branches to select which ‘Product Categories’ they stock.
This works on the face of it in terms of it is possible to relate multiple Categories to a Branch, but how the relationship field is displayed doesn’t visually show the hierarchy of the categories, which makes selecting them more difficult than it needs to be.
I have set up a simple fix for the indentation by adding acf_relationship_result to functions.php:
add_filter( 'acf/fields/relationship/result', 'acf_relationship_result', 10, 2);
function acf_relationship_result( $html, $post )
{
if($post->post_parent != 0) {
// new html
$new = ' ';
}
return $new . $html;
}
However there doesn’t seem to be a way to order the categories correctly so that children appear underneath their parent. Has anyone got a solution or workaround for this?
Hi @joeturner
I think this will be possible by using the other relationship filter:
http://www.advancedcustomfields.com/resources/filters/acf-fields-relationship-query/
This will allow you to add an order_by => ‘menu_order’:
http://codex.wordpress.org/Class_Reference/WP_Query
And should correctly order the posts!
Thanks
E
Hey
Thanks for the reply.
That helps a little. I now have this filter in place, which as you say, orders the posts by menu_order.
// filter for every field
add_filter('acf/fields/relationship/query', 'my_relationship_query', 10, 3);
function acf_relationship_query( $args, $field, $post )
{
$args['orderby'] = 'menu_order';
return $args;
}
But, this takes the menu_order as is. By this I mean, you would have a root level post with a menu_order of 1. But you also have a child of a root level post also with a menu_order of 1. This means that the posts aren’t in a hierarchical order still.
Using my example in the first post, you would now have:
1/ Electrical Items
1.1/Drills
3.1/ Bricks
1.2/Mowers
2/ Timber
3.2/ Plumbimg
3/ Building Materials
Really what I need to be able to do is create a recursive function that orders the posts under a specific post, and return them in an ordered array?
Hi @joeturner
I can see the issue and unfortunately, i don’t think it will be possible to do what you want without editing the core files.
I have recently done a lot of work with the QP_Query, ordering, and hierarchy of nested posts and found out how to correctly order the posts.
After the WP_Query is complete and you have the $posts, you need to then use a WP function to order them based on the menu/parent attributes.
This will not be possible with the current relationship field, so perhaps you can think of another solution for now?
Thanks
E
The topic ‘Hierarchical Custom Posts using Relationship custom field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.