bleem (February 29th, 2016)
// Add categories to a post based on keywords in the title.
// Modify the $title_keywords array. Use one keyword => one category name
// Categories MUST exist first. The Post title keywords will match regardless of case.
function test_add_category ($post_id = 0) {
if (!$post_id) return;
if ( $post->post_type != 'ad_listing' )
return;
$title_keywords = array ( // The keywords and the corresponding category name to add
'new iphone 5' => 'IPHONE 5',
'iphone 6' => 'IPHONE 6'
);
$title = get_the_title($post_id);
foreach ( $title_keywords as $keyword => $cat ) {
if ( stripos($title, $keyword) !== false ) {
$cat_id = get_cat_ID($cat);
}
if ($cat_id) {
$result = wp_set_post_terms( $post_id, $tags = $cat_id, $taxonomy = 'ad_cat', $append = true );
}
}
}
add_action('publish_post','test_add_category');
bleem (February 29th, 2016)
There are currently 1 users browsing this thread. (0 members and 1 guests)