Присвоить сообщение категории автоматически на основе ключевого слова заголовка сообщения

Я использовал код, приведенный ниже, чтобы назначить сообщения в определенной категории на основе ключевого слова из заголовка сообщения.

Код ниже должен идентифицировать все посты с ключевым словом addiction в заголовке и добавить их в категории с id 6 и 8.

По некоторым причинам это больше не работает.

add_action('publish_post', 'update_categories');
function update_categories(){
global $wpdb, $post;
// set the category ID to update
$categories = array(6,8);
echo "updating";
$postids = array();
// this is to tell the script to only pull posts that are labeled "publish"$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => - 1,
);
$my_query = null;
$my_query = new WP_Query($args);
if ($my_query->have_posts()):
while ($my_query->have_posts()):
$my_query->the_post();
$postids[] = $my_query->post->ID;
endwhile;
endif;

wp_reset_query(); // Restore global post data stomped by the_post().
$i = 0;
$num_of_posts = $my_query->post_count;
// running through all the posts
while ($num_of_posts > $i):
// getting the post title
$post_title = get_the_title($postids[$i]);
if (stripos($post_title, 'addiction') !== false):
// add a category
wp_set_post_categories($postids[$i], $categories, true);
endif;
$i++;
endwhile;
}

Ребята, пожалуйста, помогите?

0

Решение

Я написал код с отладкой для проверки того, что работает, а что нет. Обновите значения в соответствии с вашими потребностями и проверьте, работает ли он.

//To Add Categories to all posts containing specific word in post title
function add_categories_to_posts(){
echo "<br><br>Updating Categories Started <br><br>";

$query_args = array(
'post_type' => 'post', //post type - default post type is 'post'
'post_status' => 'publish', //published posts
'posts_per_page' => - 1, //-1 is used to get all posts
);

$all_posts = new WP_Query($query_args);

$new_categories_list = array(6,8,10,22);
$working_categories = []; //Debug : list of categoires that does not exists from given list
$not_working_categories = []; //Debug : list of categoires that exists from given list
$updated_posts_ids = [];    //Debug : list of post id's that updated categories
$not_updated_posts_ids = []; //Debug : list of post id's that did not updated any categories

//Debug : check if post categories exists
foreach ($new_categories_list as $key => $category) {
$exists = term_exists($category,'category');
if ( $exists !== 0 && $exists !== null ) {
$working_categories[] = $category;
}
else{
$not_working_categories[] = $category;
}
}

if($all_posts->have_posts()){
while ( $all_posts->have_posts() ) : $all_posts->the_post();
$title = get_the_title();
$ID = get_the_ID();
if (stripos($title, 'addiction') !== false){
$updated_posts_ids[] = $ID;
wp_set_post_categories($ID, $working_categories, true);
//set true as last argument since you don't want to override existing categories
} else{
$not_updated_posts_ids[] = $ID;
}
endwhile;
wp_reset_postdata();
}

echo "<br>Affected Post ID's array: ";
var_dump($updated_posts_ids);
echo "<br><br>Unaffected Post ID's array: ";
var_dump($not_updated_posts_ids);

echo "<br><br>Updating Categories Ended <br><br>";
}
add_action('save_post','add_categories_to_posts');
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector