我在插件中创建一个函数,当帖子移动到垃圾箱时,该函数将删除数据库行。但是,我无法使用get_posts()
获取post_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);
感谢
答案 0 :(得分:1)
您在此处使用的动作挂钩wp_trash_post
将$ post_id作为参数传递给函数。请参阅:https://codex.wordpress.org/Plugin_API/Action_Reference/trash_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);
答案 1 :(得分:0)
$ postinfo是对象。您只想要帖子的ID。所以你应该写$ postinfo-> ID。用下面的代码替换你的循环 -
foreach( $allposts as $postinfo ) {
$postinfoID = $postinfo->ID;
$wpdb->delete('rule', array('post_id' => $postinfoID));
}
答案 2 :(得分:0)
<?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);
?>