php - Using ACF Relationship field to set post type to draft or published status

admin2025-01-07  4

I am currently using the ACF Relationship field selector to query through a custom post type for locations to show various pages. These pages are "service" pages that can be shown or hidden from the end user. The challenge is if the person de-selects the service page, it is technically still visible to the user if they know the link. I need to be able to set the page to a draft status or published when updating this option. Here is what I have based on other examples related to a similar request.

function my_acf_update_value( $value, $post_id, $field  ) {
    $ids = get_field('our_services', 'options');
    $services = get_posts(array(
        'post_type' => 'services',
        'post_status' => 'draft',
        'post__not_in' => $ids,
    ));
    wp_update_post($services);
        
}

add_filter('acf/update_value/key=field_5c37e435', 'my_acf_update_value', 10, 4);

When I did this the first time it immediately set them all to draft from published without updating the ACF relationship field option. Do I need to hook into the "update" button in my options?

I am currently using the ACF Relationship field selector to query through a custom post type for locations to show various pages. These pages are "service" pages that can be shown or hidden from the end user. The challenge is if the person de-selects the service page, it is technically still visible to the user if they know the link. I need to be able to set the page to a draft status or published when updating this option. Here is what I have based on other examples related to a similar request.

function my_acf_update_value( $value, $post_id, $field  ) {
    $ids = get_field('our_services', 'options');
    $services = get_posts(array(
        'post_type' => 'services',
        'post_status' => 'draft',
        'post__not_in' => $ids,
    ));
    wp_update_post($services);
        
}

add_filter('acf/update_value/key=field_5c37e435', 'my_acf_update_value', 10, 4);

When I did this the first time it immediately set them all to draft from published without updating the ACF relationship field option. Do I need to hook into the "update" button in my options?

Share Improve this question edited Dec 17, 2021 at 20:29 user3096584 asked Dec 16, 2021 at 20:50 user3096584user3096584 212 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

There's no sense in using the init hook because this hook will be triggered on each page load.

If you want to update pages' status every time when someone updates a field, you can use any of the following hooks:

  • acf/update_field - this filter hook fires every time when a field is updated before it is saved into the database. This hook is a part of the ACF plugin.
  • update_post_meta - this action hook fires every time when a field is updated before it is saved into the database, but it is a part of the WordPress core.
  • updated_post_meta - this action hook fires every time when a field is updated after it was saved into the database. This hook is a part of the WordPress core too.

The first one passes four arguments into its handlers: $value, $post_id, $field, $original. You can learn more about it here.

And the last two hooks pass four arguments into their handlers: $meta_id, $object_id, $meta_key, $meta_value. You can learn more about them here and here.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256259a347.html

最新回复(0)