Same value for a specific custom field of all posts of a CPT

Support General Same value for a specific custom field of all posts of a CPT

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #42461
    LaurentLaurent
    Participant

    Hi guys,

    I'd like to know what the best practices are for dealing with the following situation.

    I have a CPT with 2 custom fields (one is basic text and the other is an image) that have the same value for all posts.

    The value of these CFs may change over time. Is there a way to change it in one place and have it affect all the posts in the CPT?

    Many thanks in advance!

    #42469
    PeterPeter
    Moderator

    Hello,

    If you have the same field value for all posts, you can consider using a settings page. Then change the field value in one place for all posts.

    #42472
    LaurentLaurent
    Participant

    Thank you Peter.

    Just to be sure, I didn't say all the CF are common to all posts of the CPT (only some).
    Would it still work?

    Otherwise, another option would be to simply change the value directly in the database with some magic formula (that I don't have... yet). What do you think?

    #42484
    PeterPeter
    Moderator

    Hello,

    I don't know the magic code like that. If the field value is the same for some posts not all of them, you will need to update the value manually.
    Or use the code like update_post_meta() within a loop.

    #42529
    LaurentLaurent
    Participant

    Hello Péter, I'm sorry because reading your reply, I realize my last comment mislead you.

    What I meant IS:
    - some CF of my CPT have the same value for all posts and I'd like to bulk change them from time to time,
    - others CF have a unique value for each post of this CPT.

    In that case, is the settings page still a viable option according to you?

    Or would it be the solution only if all the CF of that CPT had the dame value for all posts?

    #42541
    PeterPeter
    Moderator

    Hello,

    Use the settings page that lets you save/update the value in one place. Then you still need to use the custom code to update the value for fields in other posts. There isn't a solution with a few clicks to do that.

    I hope that makes sense.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.