Support

Account

Home Forums General Issues Change field with filter

Solved

Change field with filter

  • I have a custom field [ ‘photo_button’] it returns true or false if it is marked.
    I want to know how do a filter to enable it in all posts of a category.

  • Hi @atrevido

    I’m afraid I don’t quite understand your question. Could you please explain it to me in more details?

    If you want to update the custom field value for all of the posts in a category, I think you need to loop trough the posts and then use the update_field() function to update it. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/update_field/.

    Thanks 🙂

  • I have a custom field of a button. He can activate it or not in the post. If it is post photos it should is enabled.

    It works very well in recent posts. But I have many old posts and can not edit one by one to activate the field.

    I wonder where I apply a filter to change the true or false depending on the category of the post. My doubt is exactly where I apply the filter. ‘the_content’, ‘acf/load_field’, ‘acf/load_value’?

    I’m a bit lost about it.

    Sorry for my bad English. I am doing my best.

  • Hi @atrevido

    The acf/load_field and acf/load_value will modify the value after it’s loaded. It means that the value in the database is not changed at all. Instead, you can use the update_field() function to update your old posts. If you use the true/false field type, I believe you can do it like this:

    $theposts = get_posts(array(
        'posts_per_page' => -1,
        'post_type' => 'post',
        'category_name' => 'category-slug'
    ));
    
    foreach( $theposts as $thepost) {
        update_field('field_1234567890abc', true, $thepost->ID);
    }

    Where “field_1234567890abc”is the true/false field key and “category-slug” is the category slug of the posts you want to change.

    Please keep in mind that you only need to execute the code once. So, when you’ve done with the code, just remove it.

    I hope this makes sense 🙂

  • Thank you for your help and the explanation. Was very good

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Change field with filter’ is closed to new replies.