【问题标题】:Wordpress trying to use wp_insert_post for new posts and wp_update_post for post updates - need post ID?Wordpress 试图将 wp_insert_post 用于新帖子,将 wp_update_post 用于帖子更新 - 需要帖子 ID?
【发布时间】:2014-05-22 00:50:56
【问题描述】:

我正在构建一个 Wordpress 网站,我们希望在其中自动填充来自外部(不是 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' );

?>

【问题讨论】:

    标签: php mysql wordpress


    【解决方案1】:

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

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

    虽然这是一个迁移脚本,但也有一些用于生成 SQL 的最佳实践,例如使用 $wpdb-&gt;prepare()。也就是说,如果您收到来自 get_page_by_title() 的响应,它将是一个 $post/$page 对象,因此您可以只引用 $page-&gt;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' );
    

    【讨论】:

      【解决方案2】:

      感谢您的帮助!出于某种原因,我无法让您的代码正常工作,但是经过一些实验后,我能够让此代码正常工作。非常感谢任何额外的反馈或见解!

      <?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' );
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-20
        • 1970-01-01
        • 1970-01-01
        • 2011-06-21
        相关资源
        最近更新 更多