• Resolved Nasif

    (@nasifrr)


    Hello!

    I think I might have stumbled with a bug, but it might just be that I’m using the wrong hook for what I’m attempting to do:

    I have a hierarchical CPT called “Events”, with a couple of cmb2 metaboxes. What I want is for child events to update the publish date of their parent, so that the parent moves up in my loops (ordered by date in descending order) everytime a new child event is added.

    To achieve that, I’ve hooked onto “wp_insert_post” to run a function which checks if the event is a child. Then, gets the event’s parent, checks if the child’s date has passed (i.e. the child is published, not programmed for publishing), then checks if the child’s date is more recent than the parent’s and, finally, runs wp_update_post for the parent to clone the child’s post date:

    function update_event_parent_date($event_ID, $event){
    	if($event->post_type === 'eventos' && $event->post_parent !== 0 && $event->post_status == 'publish'){ //If it is a child event
    		$parent_event = get_post($event->post_parent); //Get the parent
    		if( $parent_event !== null && $parent_event->post_parent === 0 ){ //If the parent exists and is not the child of another post
    			$time_now = current_datetime();
    			$post_time = get_post_datetime($event_ID);
    			$parent_p_time = get_post_datetime($parent_event->ID);
    
    			if( ($time_now > $post_time) && ($post_time > $parent_p_time) ){ //Check dates 
    				//If it checks out, give the parent event the same date as the child
    				$new_date = $event->post_date;
    				$new_date_gmt = $event->post_date_gmt;
    				wp_update_post(
    					array (
    						'ID'            => $parent_event->ID,
    						'post_date'     => $new_date,
    						'post_date_gmt' => $new_date_gmt
    					),
    					false,
    					false
    				);
    			}
    		}
    	}
    }
    
    add_action('wp_insert_post', 'update_event_parent_date', 10, 2);

    The problem:

    This seems to be working fine. It updates the date when expected. However, once it does, it overwrites all of the parent event’s cmb2 meta fields with those of the child whose date was copied. It is worth noting that only the cmb2 metas are overwritten, since I’m also using Yoast SEO and those metas keep working as expected.

    I’m unsure if this could be a bug or just that I should be using another hook for this functionality, any advice or suggestion is welcome. Thanks!

    • This topic was modified 2 years, 9 months ago by Nasif.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Gonna try and see if we can recreate something like this before trying to throw some guesses out there.

    Will try to get to that asap.

    Thread Starter Nasif

    (@nasifrr)

    Thank you! Please let me know if you encounter the same problem. 🙂

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Definitely managing to recreate the issue as a whole, and my guts are telling me that it’s everything to do with all the action hooks inside wp_insert_post() and most specifically the save_post hook which is where I believe meta data is being updated/processed. Running things through wp_insert_post() the hook, and then wp_update_post() inside the callback, is invoking wp_insert_post() the function in the long run, so I’m wondering if there’s duplicate runs being done on parts.

    I’m also wondering if going the direct MySQL route may be a good alternative here. Not a complete solution ready to copy/paste, but a general idea can be gathered at https://stackoverflow.com/questions/27308459/wordpress-change-last-modified-date-of-the-post-to-post-date-scheduled-post

    At minimum, this would not run the various hooks associated with these functions, and logically should prevent the meta values from being saved to the parent too.

    Thread Starter Nasif

    (@nasifrr)

    Thanks for all your help! I don’t really know much (if anything) of MySQL so I’m a bit hesitant to go that route but I guess I can give it a try; it does seem to be the way to go to avoid any hooks from running.

    Alternately, I’m wondering if perhaps cmb2 is taking the metas from the child because the global $post is equal to the child post when I run wp_update_post(). If so, maybe overwriting it before running the function could be another method? Don’t know if that makes any sense, just a thought.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I won’t say no to that theory, but I won’t say yes that’s the source of the issue either.

    Thread Starter Nasif

    (@nasifrr)

    Fair enough. Thanks for everything! 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Possible conflict with CMB2 and wp_update_post’ is closed to new replies.