Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Fabian Suter

custom hook in inline edit

Hi Support

I’ve written the following code

add_action('acp/editing/saved', 'update_indexlink_with_permalink_on_inline_edit', 10, 3);

function update_indexlink_with_permalink_on_inline_edit($post_id, $column_id, $value) {
    // Check if the post type is 'projekt'
    if (get_post_type($post_id) === 'projekt') {
        // Update the ACF field 'indexlink' with the permalink
        $permalink = get_permalink($post_id);
        update_field('indexlink', $permalink, $post_id);
    }
}

But this is not working… What am I doing wrong?

Best

2 weeks, 6 days ago
Fabian Suter

For better understanding I’m doing this (it’s working but not with inline editing of admin columns pro):

add_action('save_post_projekt', 'update_indexlink_with_permalink', 999, 3);

function update_indexlink_with_permalink($post_id, $post, $update) {
    // If this is a revision, don't run the function
    if (wp_is_post_revision($post_id)) {
        return;
    }

    // Check if the post has the 'projekte' term in the 'sichtbarkeit' taxonomy
    $terms = get_the_terms($post_id, 'sichtbarkeit');
    $has_projekte_term = false;
    
    if (is_array($terms)) {
        foreach ($terms as $term) {
            if ($term->slug === 'projekte') {
                $has_projekte_term = true;
                break;
            }
        }
    }

    // Update the ACF field 'indexlink' with the permalink if the post has the 'projekte' term
    if ($has_projekte_term) {
        $permalink = get_permalink($post_id);
        update_field('indexlink', $permalink, $post_id);
    } else {
        // Remove the value in the 'indexlink' field if the post does not have the 'projekte' term
        delete_field('indexlink', $post_id);
    }
}
2 weeks, 6 days ago
Stefan
Developer

Thanks for your message. Based on your snippet, you have the wrong order of the parameters in the function.
The correct order for that hook is: AC\Column $column, $id, $value
You might want to check for the correct meta type as well, otherwise, a post will be updated when a user or taxonomy with the same ID is updated.

You could have a look at the third example on this page:
https://github.com/codepress/admin-columns-hooks/blob/master/acp-editing-saved.php

2 weeks, 5 days ago
Fabian Suter

Hi Stefan

Don’t get it to work :-/

Can you help me with this?

Best

2 weeks, 4 days ago
Stefan
Developer

Sure, based on your first snippet, the hook should be like this:

add_action('acp/editing/saved', 'update_indexlink_with_permalink_on_inline_edit', 10, 3);
function update_indexlink_with_permalink_on_inline_edit(AC\Column $column, $id, $value)
{
    // Check if the post type is 'projekt'
    if ($column->get_meta_type() === 'post' && $column->get_post_type() === 'projekt') {
        // Update the ACF field 'indexlink' with the permalink
        $permalink = get_permalink($id);
        update_field('indexlink', $permalink, $id);
    }
}
2 weeks, 1 day ago

You must be logged in to reply to this topic.