Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to change a post's parent via the wp_insert_post_data filter but I'm not having any luck. In the example below, I'm replacing any "parent" references with the id I want to set.

function my_insert_post_data($data, $postarr){

    $data["post_parent"] = 123;
    $postarr["parent_id"] = 123;
    $postarr["post_parent"] = 123;

    error_log(print_r($data,true));
    error_log(print_r($postarr,true));

}
add_filter( 'wp_insert_post_data' , 'my_insert_post_data' , '99', 2 );

and when i log the objects, i see the properties have been set but that never gets written to the database.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

In order to make it working you need to return updated data:

function my_insert_post_data($data, $postarr){
    $data["post_parent"] = 123;
    $postarr["parent_id"] = 123;
    $postarr["post_parent"] = 123;

    error_log(print_r($data,true));
    error_log(print_r($postarr,true));

    return $data;
}
add_filter( 'wp_insert_post_data' , 'my_insert_post_data' , 99, 2 );
share|improve this answer
    
Thanks! That makes perfect sense. All good now. –  JLeonard Jun 18 '12 at 17:25
    
@Eugene, What if you wanted to change the $postarr's tax_input>post_tag. In that case, what do we do? How do we return both the $data and $postarr? In my case, I update values in both variables... –  Average Joe Mar 21 '13 at 19:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.