我想从 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/