Home › Forums › Front-end Issues › Setting Parent Posts from Frontend
Hi there, long time lurker… generally tend to get the job done from snippets I find around here, but this has been racking my brain for a good few weeks. How can I set a parent post from the frontend? I am using elementor and acf front end with great success, but we are looking to add hierarchical support for a custom post type called myJournals. We would like to give users the ability to link parent journals but can’t seem to make it work. We have ‘hierarchical’ => true so it is working in the backend, but all of our users including editors will be limited to front-end access only.
We have set a relationship field within ACF called related_journals and have tried numerous things in the functions.php to make it work. Any chance you guys could throw us a bone and point us in the right direction?
Also, how would I go about adding a taxonomy term from a field to the post title upon creation? We would like a pre-fix added onto post title and name but it only returns taxonomy number and not the name
Before I answer the entire question. What is the return value of the taxonomy field set to? Term ID or Object?
I have tried with term id and object with the same results. I believe the term id was at least giving me the term number, whereas object didn’t show anything.
We have tried both wp_get_post_terms and get_the_terms with no luck.
object is coming back as an array, which we can’t seem to get the name of for some reason. I have tried
$term = wp_get_object_terms($post->ID, 'myterm');
$first_term = $term[0];
$term_name = $first_term->name;
$new_title = $post_id.'- '.$term_name;
to to avail. my php is quite limited so I’m sure I am not going about this the right way. post ID shows, but I either get an array, or nothing. The case above results in nothing, but if I set new_title to $term it will say array
To set the parent post from the front end you need a relationship field that is only available on the front end. Set this up to start. Add a post object field that only allows one selection.
Create the following filters for your field in functions.php
// only show field on front of site
// https://www.advancedcustomfields.com/resources/acf-prepare_field/
add_filter('acf/prepare_field/name=YOUR FIELD NAME HERE', 'field_only_on_front');
function field_only_on_front($field) {
if (is_admin()) {
return false;
}
return $field;
}
// alter the acf query of the post object field to only show top level posts
https://www.advancedcustomfields.com/resources/acf-fields-post_object-query/
add_filter('acf/fields/post_object/query/name=YOUR FIELD NAME HERE', 'only_top_level_posts', 20, 3);
function only_top_level_posts($args, $field, $post_id) {
$args['post_parent'] = 0;
return $args;
}
After you do this set up your taxonomy field to return term objects
Once all that is done then then create an acf/save_post filter in functions.php
add_action('acf/save_post', 'update_and_save_journal_post');
function update_and_save_journal_post($post_id) {
// make sure to only do this on the right post type
$post_type = get_post_type($post_id);
if ($post_type != 'YOUR POST TYPE HERE') {
return;
}
// get the post
$post = get_post($post_id);
// get the parent, unformatted, ID only
$parent = get_field('YOUR POST OBJECT FIELD', $post_id, false);
if ($parent ) {
$post->post_parent = $parent;
}
// get the taxonomy field
$terms = get_field('YOUR TAXONOMY FIELD;, $post_id);
if (!empty($terms) && !is_wp_error($terms)) {
$term = terms[0];
$post->post_title = $term->name.$post->post_title;
}
// remove this action so that an infinite loop is not created
remove_filter('acf/save_post', 'update_and_save_journal_post');
// update the post
wp_update_post($post);
// re-add this filter
add_action('acf/save_post', 'update_and_save_journal_post');
}
Please note that none of the above code has been tested for syntax errors.
You must be logged in to reply to this topic.
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’re hard at work on ACF 6.1, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.
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.