Home › Forums › Feature Requests › Relationship Fields set as $post->post_parent
I’m attempting to create a relationship between two CPTs with the intention of creating a custom permalink structure. For example, I have Maps
& Stories
, and each story
will be a child of a map
. So my permalink structure will eventually look something like: http://domain.com/map-name/story-name
Now in order to accomplish this rewrite rule I need to set the map post_id as the $post->post_parent
of the story. I’m wondering if its possible to use a relationship field but instead of storing the value to the acf-field
cpt or some form of meta data can it stored as the post_parent
?
Trying to put something together this week so I realize this may not already be a possibility for ACF, so perhaps this is a feature request, but if that’s the case is there a filter I can add to accomplish this in the mean time?
Thanks so much, using ACF for years now and can’t believe how far its come, I refuse to use other options out there even though I know “Types” as a way of creating this relationship already.
Also here is a post that outlines what I’m attempting currently:
How to set parent-child relationship between differents custom post types
Not sure if you’re still looking for help with this or not, but I think all that is necessary is a way to set the parent post_id of the child and WP does the rest. I’m looking at this for future projects myself.
My thinking is to create an acf/save_post action http://www.advancedcustomfields.com/resources/acfsave_post/ that runs after the post has been saved and after the ACF action (priory > 10), get the value from the field and update the post to set the parent.
I was able to get this figured out through a couple stack exchange forums. I did not end up using an ACF field because I wasn’t sure at the time. Essentially I have a rewrite rule hooked to init
and post_type_link
that uses the meta value of my custom field (which I imagine can be done with ACF as well. Here’s some of the code:
/* Add rewrite rules for permalink structure */
add_action( 'init', array( $this, 'map_rewrite_rules') );
add_filter( 'post_type_link', array( $this, 'filter_map_permalik'), 10, 2 );
public function setup_map_meta_boxes() {
add_meta_box('story-map', 'Map Series', 'story_attributes_meta_box', 'story', 'side', 'default');
}
public function map_rewrite_rules() {
add_rewrite_rule( '(.*)/([^/]+)/?$','index.php?story=$matches[2]','top' );
}
I made the custom field like so:
function story_attributes_meta_box($post) {
$pages = wp_dropdown_pages(
array(
'post_type' => 'map',
'selected' => $post->post_parent,
'name' => 'parent_id',
'show_option_none' => __('(no parent)'),
'sort_column' => 'menu_order, post_title',
'echo' => 0
)
);
if ( ! empty($pages) ) {
_e('<label class="fat-wide">Map: </label>');
echo $pages;
}
}
Hope that can help you on your project in some way
Glad you found a solution, and thank you for posting it here. I’m sure that others will find it helpful in the future.
The topic ‘Relationship Fields set as $post->post_parent’ 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.