• Hello,

    The situation:
    – i have installed a wordpress which works well at this domain: https://www.custom-domain.com
    – i would like to add a page, for example https://www.custom-domain.com/up.php, which update some posts on loading.

    As it is temporally and i get hundreds posts to update, i don’t want to use the back-office and neither an import plugin. I get the field to update as a plain text list. An additional difficulty, some filed are ACF ones and not yet fill in the post (the row doesn’t exist in the wp_postmeta table).

    So i create up.php in the root WP directory like that:

    <?php
    
    require( dirname( __FILE__ ) . '/wp-blog-header.php' );
    
    $my_post = array(
    array(
    	'ID' => 9924,
    	'chapo' => 'Lorem Ipsum',
    	'_chapo' => 'field_5458ff1e418c7', // ACF Field
    ),
     array(
    	'ID' => 12039,
    	'chapo' => 'Zombie Ipsum',
    	'_chapo' => 'field_5458ff1e418c7',
    ),
    );
    
    function update($post_list) {
    	foreach ( $post_list as $post ) {
    		$post_ID = $post['ID'];
    		$post_title = get_the_title($post_ID);
    		wp_update_post( $post );
    		// update_post_meta( $post_ID, 'chapo', $post['chapo'] );
    		printf ("%u - %s ok<br/>",$post_ID,$post_title);
    	}
    };
    
    update($my_post);
    
    ?>

    The script seems to works but the wp_update_post doesn’t. My custom field is not update.

    Could it be a problem with the WP function? With the position of the page?
    Could it be a problem of right?
    Do i have to change the way of doing this?

    Thank you in advance for any help.
    jbo

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    wp_update_post() doesn’t do anything because $post has no usable data other than ID. The function does not update post meta, you need to use update_post_meta() for that, but you have it commented out.

    Thread Starter jbo.ciel

    (@jbociel)

    Thank you bcworkz for your answer.

    I try to use update_post_meta() like this:

    //wp_update_post( $post );
    update_post_meta( $post_ID, 'chapo', $post['chapo'] );

    … and nothing happen.

    I am still investigating.

    Moderator bcworkz

    (@bcworkz)

    I think update() is executing before WP has finished loading. Try adding it as a callback to the ‘init’ action instead of calling it directly.

    You will not be able to pass $my_post to an action callback. For now, move the $my_post declaration to inside the update() function. For the real thing you’ll need to come up with a way for the function to get the data to update. I’m not sure what you originally had in mind. An external CDF or JSON data file that is opened and read is one possibility.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘custom page in php to update multiple posts’ is closed to new replies.