• I have a fairly complex (I think) problem:

    Say I have 5 posts, all with a post_meta key of bid_id and all having the same value of 4. Other posts will have different values such as 6, 5, 10 etc.
    The 5 posts also have a post_meta key of is_awarded and values that can be either not_yet, yes or no. All posts will initially be set as not_yet.

    When a post has the is_awarded value changed to yes, I would like all of the other posts with the bid_id of 4 to automatically change their is_awarded value to no.

    I’m guessing I will need some sort of fairly complex function to get this happening but have no idea where to begin.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The WP_Query class supports the ability to query posts by their post meta value. You can create a new query object looking for posts with a specific bid_id and the is_awarded value of not_yet, then iterate through those posts, setting the post meta for the found posts to no.

    I hope this helps!

    Create a function that hooks into edit_post hook, read the values form the $_POST array, if is_awarded set to yes update values in the database. ??
    https://pastebin.com/3g2xNMjk

    Thread Starter snails07

    (@snails07)

    Thanks for the responses.
    Fire Truck, that code isn’t working. It doesn’t change any of the other meta values in the database.
    I’ve tried to work out where it is going wrong but that code is a bit over my head.
    Any other ideas??

    ^^ Works for me. ??
    Backup the wordpress database.

    Add the lines

    echo '<pre>';
    print_r($_POST);
    echo '<hr>';
    print_r($post);

    to the end of my function do_edit_post, change is_awarded to yes, press Update button and post the content that will appear on the screen in the Pastebin.

    Note: this will temporarily break the edit function. ??

    Thread Starter snails07

    (@snails07)

    I should also mention that I am using the Front End edit plugin to edit the meta data on the front of the site, not in the admin area.

    Would this be making a difference to the function?

    Here is what the function looks like with the above code pasted in – https://pastebin.com/mGAg6eVE and it displays nothing on the site.
    (Note that I have now changed the names of the meta_keys since my original post)

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Changing post_meta based on other posts post_meta’ is closed to new replies.