我正在为Wordpress构建一个插件,从XML提要中提取数据,并使用wp_insert_post()函数发布所有数据。插件每小时执行一次,所以我必须防止双重发布。
我试着添加一个过滤器,并将XML提要中的post_date与Wordpress中的post-date进行比较(因为我给帖子的post_data与XML中的相同),但它不起作用,我不明白为什么。。
这是我的代码:
add_filter('posts_where', 'checkPosts'); //I add a filter
$query = new WP_Query('post_type=event'); // Make a query for the custom post_type 'event'
if(!$query->have_posts()) { //If it doesn't have any posts with the same post_date post it
$post_id = wp_insert_post($post);
wp_set_object_terms($post_id, $genres, 'genre');
}
remove_filter('posts_where', 'checkPosts');
function checkPosts($where = '') {
$where .= " AND post_date = ".$post_date;
return $where;
}
有人能告诉我我的错误吗?或者给我另一种技巧来防止Wordpress中出现相同的帖子吗?
我对Wordpress过滤器不是特别熟悉,但我不认为双等号是有效的SQL。
尝试更改您的过滤功能:
function checkPosts($where = '') {
$where .= " AND post_date = ".$post_date;
return $where;
}
好吧,所以如果有人试图做同样的事情,这对我来说是有效的。
我删除了过滤器,因为我不能100%确定如何使用它们,而且使用这种方法可以更快/更容易地实现:
$query = new WP_Query('post_type=event&year='.$post_Y.'&monthnum='.$post_m.'&day='.$post_d.'&hour='.$post_H.'&minute='.$post_i);
if(!$query->have_posts()) {
$post_id = wp_insert_post($post);
wp_set_object_terms($post_id, array( $venue_name ), 'locatie');
}
只需在年份、月数等中填写您想要使用的信息即可!