Php 如何获取删除帖子的所有帖子id?Wordpress
我在插件中创建了一个函数,当帖子移动到垃圾箱时,该函数将删除数据库行。但是,我无法使用Php 如何获取删除帖子的所有帖子id?Wordpress,php,wordpress,Php,Wordpress,我在插件中创建了一个函数,当帖子移动到垃圾箱时,该函数将删除数据库行。但是,我无法使用get\u posts()获取帖子id 这是我的密码: function delete_condition($post) { global $wpdb; $allposts = get_posts(array( 'numberposts' => -1, 'category' => 0, 'orderby' => 'date',
get\u posts()
获取帖子id
这是我的密码:
function delete_condition($post)
{
global $wpdb;
$allposts = get_posts(array(
'numberposts' => -1,
'category' => 0, 'orderby' => 'date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'job',
'suppress_filters' => true));
foreach( $allposts as $postinfo ) {
$wpdb->delete('rule', array('post_id' => $postinfo));
}
}
add_action('wp_trash_post', 'delete_condition', 10, 1);
谢谢$postinfo是对象。您只需要post的ID。所以你应该写$postinfo->ID。用下面的代码替换你的循环-
foreach( $allposts as $postinfo ) {
$postinfoID = $postinfo->ID;
$wpdb->delete('rule', array('post_id' => $postinfoID));
}
这里使用的操作挂钩,
wp\u trash\u post
,将$post\u id作为参数传递给函数。见:
听起来像是要从一个表中删除与被丢弃的表具有相同post ID的所有行
我想你可能想写这样的东西:
function delete_condition( $post_id ) {
global $wpdb;
// Delete rows in the rule table which have the same post_id as this one
if ( 'job' === get_post_type( $post_id ) ) {
$wpdb->delete('rule', array('post_id' => $post_id ) );
}
}
add_action('wp_trash_post', 'delete_condition', 10, 1);
<?php
function delete_condition($post)
{
global $wpdb;
$allposts = get_posts(array(
'numberposts' => -1,
'post_status' => 'any',
'category' => 0, 'orderby' => 'date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'job',
'suppress_filters' => true));
foreach( $allposts as $postinfo ) {
$wpdb->delete('rule', array('post_id' => $postinfo));
}
}
add_action('wp_trash_post', 'delete_condition', 10, 1);
?>