Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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++;
 }
share|improve this question
add comment

1 Answer

The first code will only do it for posts matching your query - i.e posts published that day. Something like

$query = new WP_Query( 'post_type=post' );

should retrieve all the posts, and then you could loop through them and perform the necessary operation.

A word of caution. Whenever a post is published, your function is run - so with every new published post, all your posts are checked and (if necessary) added to the category. To save time / unnecessarily querying the database, you might want to use the first function only once (maybe trigger it with a button click) and then use the second function for all new posts, which only check the post being published.

Answer to updated question If you want to schedule to repeat the function then use:

<?php wp_schedule_event($timestamp, $recurrence, $hook, $args); ?>

$timestamp is a UNIX timestamp for when the scheduled first occurs. $recurrence is a string to describe how regular it should happen (e.g. 'daily', 'hourly' etc), $hook is your function name as a string, i.e. 'update_post_terms'. Lastly $args is an array of (optional) arguments for your function.

As it stands though, your function takes $post_id as an argument and only updates that post. You would perhaps need another function (which is called periodically) to loop through all your posts and call the above function, if you wanted to update all posts (daily, hourly etc).

See the Codex page for more detail.

Hope this helps!

share|improve this answer
    
i know this is going to run every time on page load but is it possible to do it like one instance every two hours automatically, like some rss aggregator plugins do, i updated the above post by the way....but first i have to figure out to run it every time on page load for existing posts –  Ravi Oct 18 '11 at 23:14
    
Please help me figure out one thing at a time, i'v added another update above to pull all the post id's now, Kindly see what iam doing wrong?? –  Ravi Oct 19 '11 at 8:52
    
Remove add_action( 'wp_insert_post', 'update_post_terms' ) from the loop. At the bottom change !==false to ==true or !=false. Finally wrap it inside a function and call it when necessary (i.e. every so often, or on 'init' etc) –  Stephen Harris Oct 19 '11 at 9:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.