gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:13:31 28 4
gpt4 key购买 nike

我正在构建一个 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' );

?>

最佳答案

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

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

虽然这是一个迁移脚本,但也有一些生成 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' );

关于php - Wordpress 尝试将 wp_insert_post 用于新帖子,将 wp_update_post 用于帖子更新 - 需要帖子 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23796124/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com