-1

I have an external script I wrote (for admins only) that loads in the following at the top:

require_once('./../wp-blog-header.php');

I'm aware that it can be a plugin, but it's just not right now.. it's just a standard php script.

Inside this script I have a form where it allows the person viewing to update a title.

Here is my code when they submit the form:

$post_data = array(
  'ID'         => $_POST['post_id'],
  'post_title' => $_POST['post_title']
);
wp_update_post($post_data);

wp_update_post() actually returns an ID, which makes me think its working, but the post simply doesn't change, and the title never updates.

What might I be missing here? At the very least I would expect an error if it doesn't work.

I'm positive the ID and the Title are correct/filled in.

  • Are you updating revision IDs, perhaps? – s_ha_dum Apr 30 '14 at 16:52
  • I'm finding the ID by using $post->ID. Would that always return the actual post, or could that return revision IDs? – Tallboy Apr 30 '14 at 17:00
  • Your code is very out of context. I can't say what is happening really. – s_ha_dum Apr 30 '14 at 17:05
0

Please use the following code to update the title.As the frist step include the database files.

require_once 'wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';

Then use the following code to udpate wordpress title.

 $updatePost = array(   
        'ID' => $postCSVContent[0], // wordpress Id
        'post_title'    => $postCSVContent['1'], // Updated title
        'post_content'  => $postCSVContent['2'], // Updated content
        'post_type' => 'page',
        'post_status'   => 'publish',
        'post_author'   => 1
    );

    wp_update_post( $updatePost );

Please also refer the tutorial that explain how to read data from CSV and udpate the post externally. http://www.pearlbells.co.uk/code-snippets/insert-update-wordpress-post-programmatically/

0

Sometimes I use this code when I want to run WordPress externally.

update.php

function find_wordpress_base_path() {
    $dir = dirname(__FILE__);
    do {
        //it is possible to check for other files here
        if( file_exists($dir."/wp-config.php") ) {
            return $dir;
        }
    } while( $dir = realpath("$dir/..") );
    return null;
}

define( 'BASE_PATH', find_wordpress_base_path()."/" );
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . 'wp-load.php');

$post_id = $_POST['id'];
$title = $_POST['title'];

// Update post
$my_post = array(
    'ID'           => $post_id,
    'post_title'   => $title, // new title
);

// Update the post into the database
wp_update_post( $my_post );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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