I’ve been looking into this lately as well (how to allow a user to delete on the frontend without having wp-admin access).
The ACF method works well, but also wanted to point out this:
You can use the normal WP delete post function, which requires wp-admin access, but redirect them away just after the delete function runs. So they never see wp-admin, but are still able to have the functions run.
Hopefully this is helpful to someone, as it was to me.
Ok thank you for some of your logic help on this.
What I ended up doing is adding code on the single-CPT.php template that does this:
$client_name = get_the_terms(get_the_ID(),'client-name');
$post_author_id = get_the_author_meta('ID');
$term_id = $client_name[0]->term_id;
if(!get_term_meta($term_id,'term_author_id')) {
add_term_meta($term_id, 'term_author_id', $post_author_id, true);
}
So, on the post, it get’s the term from the taxonomy (this post type will only ever have ONE term assigned to it from that taxonomy). Then it checks if the term has the term meta already, and if it doesn’t, it assigns the author ID of that post as the term_author_id value. That way the code only runs if it needs to.
I decided on this method, since when the user creates a post in this post type, they have to also assign an existing term, or create one, and then is redirected to that post after creation. So that single template file will load immediately after the post is created, this code will run, and everything seems to be working as intended.
My current issue is in the ACF field where the user picks the Taxonomy Term, it is pulling in ALL of the terms still. I have written code to only show terms in other areas:
$taxonomy = 'client-name';
$terms = get_terms($taxonomy, array(
'hide_empty' => false,
'meta_key' => 'term_author_id',
'meta_value' => get_current_user_id()
));
This works great to only display the terms for the logged in user in other areas… and I thought I saw a WP filter that I could apply this logic to EVERYWHERE the terms would be displayed… but now I can’t find it.
Any idea on how this could be done in the ACF field at least?
Perhaps this?
https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
Or is there a better way?
The goal is to add the user ID as term meta when a term of a custom Taxonomy is created.
Since I’m using an ACF field for the frontend user (logged in only) to be able to add terms if needed, I thought the acf/save_post hook might be easiest. I’m just unsure of how to exactly write the code.
I am good with editing php, if I have an example of something close to what I’m wanting… but I’ve just been coming up empty on this particular need.
$term_id = Not sure where I'd get this from within a hook during term creation.
if ($term_id is new) {
$user_id = get_current_user_id();
add_term_meta($term_id,$user_id,'term_author_id',true); //term would be unique, and have unique meta (unique author)
}
I’m not sure I’m even on the right track with my logic.
When a post is being created or edited, if a new term has been created, that’s when I want to add the ID of the user that is doing it as term meta… so that I can later show terms made by that author back to them.
If you could provide an example, or even the exact code you think would do it that would be AWESOME.
Thank you!
Awesome! Thanks for the suggestion to look in my account. There was a free upgrade to Pro sitting there and waiting for me.
Used this to upgrade to Pro properly
https://www.advancedcustomfields.com/resources/upgrading-v4-v5/
All went well, and the title field is showing, and the custom post was created via the form.
YAY! Thanks again.
Using Version 4.4.7
I read here that ACF Pro has to be used just to get the title field:
Is this true? I have bought other extension packs, but I have to get pro to make this work?
Is there a workaround that I could do, like setting a default title, or have the title be dynamically generated from some of the custom fields that are filled out? If not, I’ll just get Pro. Thanks for your help on this.
Ah, I didn’t know I had to declare the field group. The example didn’t include it, so I wasn’t sure. Thanks!
This is in a page.php template, but is for creating a post within a CPT ‘real-wedding’.
So, now I have the fields showing up by adding the field_group:
<?php acf_form(array(
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'real-wedding',
'post_status' => 'pending'
),
'post_title' => true,
'field_groups' => array('13612'),
'submit_value' => 'Submit'
)); ?>
The post_title field doesn’t show still though. Is there something else that needs to be done for that? Does it not show because the form isn’t embedded in the single-real-wedding.php file?
It was a conflict with a plugin. We had initially thought the use of https://wedevs.com/products/plugins/wp-user-frontend-pro/ was what would be needed, and so it was still activated. Upon deactivating it, everything is working fine.
Thanks!
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.