Wordpress试图将wp_insert_post用于新帖子,wp_update_post用于帖子更新 - 需要帖子ID吗?

时间:2014-05-22 00:50:57

标签: php mysql wordpress

我正在构建一个Wordpress网站,我们希望从EXTERNAL(非wordpress)数据库中自动填充wordpress帖子。我已经创建了一个代码,可以成功检查外部数据库的帖子标题,如果找不到,则创建帖子。这很棒,但如果找到标题,我也会尝试“更新”帖子。我试图更新我的代码来执行此操作,但是在我的插件中运行代码时出现错误,说明

Notice: Undefined variable: page_name in /var/www/html/dev2/wp-content/plugins/hello/hello.php on line 54 Fatal error: Cannot redeclare get_page_id() (previously declared in /var/www/html/dev2/wp-content/plugins/hello/hello.php:49) in /var/www/html/dev2/wp-content/plugins/hello/hello.php on line 49

任何人都可以帮我修改我的代码以插入新帖子和更新旧帖子吗?这是我的代码,非常感谢您的专业知识!!!

<?php
/*
Plugin Name: Post Updater
Plugin URI: http://wordpress.org/
Description: 
Author: Matt
Author URI: 
Version: 2.3
Text Domain: 
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

// Add Shortcode
function hello() {

    // Code
global $wpdb;

$sql = <<<SQL
SELECT cool_name, cool_content
FROM cool_test.cool_table 
SQL;




$rebates = $wpdb->get_results( $sql );
foreach ( $rebates as $rebate ){
    $new_post = array(
        'post_title' => $rebate->cool_name,
        'post_content' => $rebate->cool_content,
        'post_status' => 'publish',
        'post_date' => date('Y-m-d H:i:s'),
        'post_author' => 31,
        'post_type' => 'post',
        'post_category' => array(10)
    );
    $page_exists = get_page_by_title( $rebate->cool_name, OBJECT, 'post' );

    if( $page_exists == null ) {
        // Page doesn't exist, so lets add it
        $insert = wp_insert_post( $new_post );
        if( $insert ) {
            // Page was inserted 
        }
    } else {
        // Page already exists
        function get_page_id($page_name){
    global $wpdb;
    $page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
}
        $updatePost = array(
      'ID'           => $page_name,
      'post_content' => $rebate->cool_content
  );

// Update the post into the database
  wp_update_post( $updatePost );
    }


}


}
add_shortcode( 'hello', 'hello' );

?>

2 个答案:

答案 0 :(得分:1)

首先,为一次性迁移脚本使用短代码的有趣技巧:)。

您看到的错误是因为您在function get_page_id($page_name)循环中声明foreach,这就是为什么您收到有关它未定义的错误的原因 - 它尝试每个循环定义一次,所以它第二次迭代失败。

虽然这是一个迁移脚本,但是生成SQL也有一些最佳实践,例如使用$wpdb->prepare()。也就是说,如果您收到来自get_page_by_title()的回复,那么它将是$post/$page个对象,因此您只需引用$page->ID而不是进行其他查询。

清理版代码:

// Add Shortcode
function hello() {
    global $wpdb;
    $rebates = $wpdb->get_results( "SELECT cool_name, cool_content FROM cool_test.cool_table" );
    foreach ( $rebates as $rebate ){
        $page = get_page_by_title( $rebate->cool_name, OBJECT, 'post' );
        if( null === $page ) {
            // Page doesn't exist, so lets add it  
            $new_post = array(
                'post_title' => $rebate->cool_name,
                'post_content' => $rebate->cool_content,
                'post_status' => 'publish',
                'post_date' => date('Y-m-d H:i:s'),
                'post_author' => 31,
                'post_type' => 'post',
                'post_category' => array(10)
            );

            ;
            if( false !== ( $new_id = wp_insert_post( $new_post ) ) ){
                // Page was inserted, the new page ID is $new_id
            }
        } else {
            // Page already exists
            $updatePost = array(
                'ID'           => $page->ID,
                'post_content' => $rebate->cool_content
            );

            // Update the post into the database
            wp_update_post( $updatePost );
        }
    }
}
add_shortcode( 'hello', 'hello' );

答案 1 :(得分:0)

谢谢你的帮助!出于某种原因,我无法使您的代码工作,但经过一些实验,我能够使这些代码正常工作。非常感谢任何其他反馈或见解!

<?php
/*
Plugin Name: Incentive Post Updater
Plugin URI: http://wordpress.org/
Description: 
Author: Matt
Author URI: 
Version: 2.3
Text Domain: 
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

// Add Shortcode
function hello() {

    // Code
global $wpdb;

$sql = <<<SQL
SELECT cool_name, cool_content
FROM cool_test.cool_table 
SQL;




$rebates = $wpdb->get_results( $sql );
foreach ( $rebates as $rebate ){
    $new_post = array(
        'post_title' => $rebate->cool_name,
        'post_content' => $rebate->cool_content,
        'post_status' => 'publish',
        'post_date' => date('Y-m-d H:i:s'),
        'post_author' => 31,
        'post_type' => 'post',
        'post_category' => array(10)
    );
    $page_exists = get_page_by_title( $rebate->cool_name, OBJECT, 'post' );

    if( $page_exists == null ) {
        // Page doesn't exist, so lets add it
        $insert = wp_insert_post( $new_post );
        if( $insert ) {
            // Page was inserted 
        }
    } else {

    $updatePost = array(
                'ID'           => $page_exists->ID,
                'post_content' => $rebate->cool_content
            );

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

    }


}
echo "cool";

}
add_shortcode( 'hello', 'hello' );

?>