Editing custom post type

Hi there,

I’m working on a “Edit resource” form which loads 1 single page: mysite.com/r/edit
I was thinking about adding a button to the single post page which only shows when the author views the page, then that would link to: /r/edit?resource_id=123

However, I do not want people to be able to edit any post by just changing the id in the URL since I’ll be using {url_parameter:resource_id} to grab the post ID to load on the /r/edit page.
I dont want to work with popup’s or anything user unfriendly like that.

Does anyone here maybe have an idea on how to properly load post information in the form for editing dynamically? Of course only if the user owns the post.

Thanks in advance!
Roel

Small update, I’ve changed the “resource_id” to “r_id” for short.
I also added this code to my functions.php:

function custom_fetch_post_data($field) {
    // Ensure we have the r_id in the URL
    if(!isset($_GET['r_id'])) {
        return '';
    }

    $post_id = intval($_GET['r_id']);
    $post_data = get_post($post_id);

    // Check for user authorization
    $current_user = wp_get_current_user();
    if($post_data->post_author != $current_user->ID) {
        return ''; // Don't provide data if not the author
    }

    switch($field) {
        case 'post_title':
            return $post_data->post_title;
        case 'post_content':
            return $post_data->post_content;
        case 'featuredImage':
            return get_post_meta($post_id, 'featuredImage', true);
        default:
            return '';
    }
}

However, using the “Update Post” action does not seem to work at all. Also “Update Post Meta” doesn’t work.

Edit; It submits but the post doesn’t get updated.
I use the following dynamic data to pre-populate the fields:

{echo:custom_fetch_post_data('post_title')}
{echo:custom_fetch_post_data('featuredImage')}
{echo:custom_fetch_post_data('post_content')}

Any idea @Daniele ?

@Daniele
I figured it out. The ‘Update Post’ action requires you to set a form field. It does not accept dynamic data like {url_parameter:r_id}. Is this a bug?

Also, it resets the post date to first of january 1970 when no post_date is given, even when it’s just updating the post.

Have you tried using the “Update Post Meta” action? Only works for acf fields and all that but I think it accepts dynamic data and at least in my case, left the date untouched. Or maybe the “Update Option” action would be an option, no pun intended.

Would that work? Since post_title and post_content aren’t post meta I believe, the only post meta field I want users to update is a custom field named featuredImage.

Maybe I’m mistaken. Also, I use Metabox and not ACF

Metabox vs ACF doesn’t matter but yeah if it’s the post title you’re after I guess you’ll have to stick with the post update action. Just tested updating the title using update post and my date wasn’t affected.

Admittedly I haven’t properly looked ay everything but wouldn’t it be easier to set a condition on the form which checks the currently logged in user ID against the one of the author?

I think it has to do that I’m retrieving the Post information through functions instead of on the same page.
I’m also doing a check of the logged in user ID against the author’s ID based on the Post ID so that’s all good.

Thanks for sharing your thoughts! This was a real brain scratcher but it’s working flawlessly now.

yeah was just thinking the same after re-reading your earlier posts. My thoughts were useless but I’m glad you’ve figured it out haha, brainscratchers are always the most fun to figure out :smiley:

1 Like

@manc Have you tried updating the ACF options page fields? I used both “update post meta” and "update option " actions, but its not working for me.