php - wordpress 中的错误插入到自定义表值中为空或 null

标签 php mysql sql wordpress

我想从 wordpress 帖子中获取所有帖子的标题并插入到自定义表格中,但它总是插入空值,为什么

<?php
global $post;
$args = array( 'numberposts' => -1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : 
setup_postdata( $post ); 
insert_p_now(the_permalink());
endforeach;
wp_reset_postdata(); 

function insert_p_now($a)
{
    global $wpdb;
    $wpdb->insert('wp_posttable', array('post_title' => $a), array('%s'));
}
?>

最佳答案

在您的函数调用中,您没有将帖子标题作为参数传递。

insert_p_now(the_permalink());

意思是在你的函数中

function insert_p_now($a)
{
    global $wpdb;
    $wpdb->insert('wp_posttable', array('post_title' => $a), array('%s'));
}

$a 的值等于 the_permalink() 当然不是你的 post_title。 如果您只想将帖子标题存储在自定义数组中,也许这就是您要查找的内容:

<?php

global $wpdb;

$args = array( 'numberposts' => -1 );
$allposts = get_posts( $args );

foreach ( $allposts as $curpost ) : 
   $wpdb->insert('wp_posttable', array('post_title' => $curpost->post_title), array('%s')); 
endforeach;

?>

如果您要重复使用“插入自定义表格”功能,您当然可以使用单独的函数。

希望对您有所帮助!

关于php - wordpress 中的错误插入到自定义表值中为空或 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25947645/

相关文章:

java - 运行长 MySQL 线程时,Spigot 服务器 "stops responding "

sql - 并发插入实体集合

python - cs50 pset7包含语法

mysql - sql : allow same period for just two entries

php - 用php替换大文件中的字符串

php - php-fpm 在哪里获取其在 osx 上的配置值

javascript - 如何在动态表中计算折扣、税(增值税)、小计和总计

php - Paypal Express Checkout 负面测试

php - MySQL语法错误

mysql - 在mysql中连接两个表