how can i query posts by current date and auto assign category based on keyword found on post title, something like if title contains 'oranges' auto assign it 'oranges' category
maybe something like this which can go to functions.php
function add_category_automatically($post_ID) {
global $wpdb,$post;
$today = getdate();
$query = new WP_Query( 'year=' . $today["year"] . '&monthnum=' . $today["mon"] .'&day=' . $today["mday"] );
while ( $the_query->have_posts() ) : $the_query->the_post();
$post_title = get_the_title();
if (stripos($post_title, 'oranges')!==false) {
$cat = array(8);
wp_set_object_terms($post_ID, $cat, 'category');
}
endwhile;
}
add_action('publish_post', 'add_category_automatically');
the above code doesn't work although this code below works but it doesn't really do it for existing posts until i update them again, so i want a function to query all the posts by current date and it should only run if found new posts for today
function add_category_automatically($post_ID) {
global $wpdb,$post;
$post_title = get_the_title();
if (stripos($post_title, 'oranges')!==false) {
$cat = array(8);
wp_set_object_terms($post_ID, $cat, 'category');
}
}
add_action('publish_post', 'add_category_automatically');
Update:
i am using this code now , it works if i publish a new post but doesnt work for existing posts??? ..... it should run every time and check all the posts in the database and auto categorize the post but smhw its not
add_action( 'wp_insert_post', 'update_post_terms' );
function update_post_terms( $post_id ) {
if ( $parent = wp_is_post_revision( $post_id ) )
$post_id = $parent;
$post = get_post( $post_id );
if ( $post->post_type != 'post' )
return;
$post_title = get_the_title();
if (stripos($post_title, 'oranges')!==false) {
// add a category
$categories = wp_get_post_categories( $post_id );
$newcat = get_term_by( 'name', 'oranges', 'category' );
array_push( $categories, $newcat->term_id );
wp_set_post_categories( $post_id, $categories );
}
}
i know it will put a good load on my database even if i figure this out but i am looking for a way to do this like one instance every two hours or something like that, Please help!
Update 2 to check all posts and loop them to push the category
$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,
'caller_get_posts'=> 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;
}
wp_reset_query(); // Restore global post data stomped by the_post().
$i = 0;
while ($num_of_posts > $i){
$post_id = $postids[$i];
add_action( 'wp_insert_post', 'update_post_terms' );
function update_post_terms( $post_id ) {
if ( $parent = wp_is_post_revision( $post_id ) )
$post_id = $parent;
$post = get_post( $post_id );
if ( $post->post_type != 'post' )
return;
$post_title = get_the_title();
if (stripos($post_title, 'oranges')!==false) {
// add a category
$categories = wp_get_post_categories( $post_id );
$newcat = get_term_by( 'name', 'Oranges', 'category' );
array_push( $categories, $newcat->term_id );
wp_set_post_categories( $post_id, $categories );
}
}
$i++;
}