• This sounds wacko, But is there a way to assign keywords to each category so that if the keyword appears in a post body or title wordpress automatically assigns it to the specified category?

    My girlfriend has one of the top 20 fashion blogs in a her country and wants to move her blog to a self-hosted WordPress, but she wants very little to do with actually using WordPress itself.

    Since her platform of choice is Instagram I set everything up so that whatever she Instagrams gets automatically posted to her Facebook page, which then gets auto-posted to her blog as a regular post. This actually works amazingly well, but of course this means that she cannot assign categories unless (I thought) she could just insert a keyword into the title which would trigger an automatic wordpress categorization.

    Is this possible at all?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I think this may help. It has not been tested, but should be close.

    The code below should be added to functions.php. Add the keywords and category names that you need to match.

    // Add a 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 mam_add_category ($post_id = 0) {
       if (!$post_id) return;
       $title_keywords = array ( // The keywords and the corresponding category name to add
         'Germany' => 'Germany',
         'Phd' => 'PHD',
       );
       $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 = 'category', $append = true );
          }
       }
    }
    add_action('publish_post','mam_add_category');

    It’s working for me, thank you for your help. i did a little modification on you code. I removed the last “,” after PHD

    ‘Phd’ => ‘PHD’, –> ‘Phd’ => ‘PHD’

    So the right code for me is :

    // Add a 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 mam_add_category ($post_id = 0) {
       if (!$post_id) return;
       $title_keywords = array ( // The keywords and the corresponding category name to add
         'Germany' => 'Germany',
         'Phd' => 'PHD'
       );
       $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 = 'category', $append = true );
          }
       }
    }
    add_action('publish_post','mam_add_category');

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    Next step is create a page in the back office to add keywords in the backoffice. 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘make post title determine category?’ is closed to new replies.