WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] A WordPress "news feed" (7 posts)

  1. Blandanomics
    Member
    Posted 3 years ago #

    Is it possible to have wordpress create a post whenever a category or custom taxonomy is created?

    For example if I were to create "Google" in the taxonomy of "Company" is there any way to create a post titled "Google has been added in Company"?

    Or, at the very least, have it outputted as a feed or text? I'd like to keep my readers updated as I add companies to our directory.

    I'm not asking for some kind soul to write the code for this for me (though that would be awesome), I more want to ask those more familiar with WP if it is even possible.

  2. Reuben Gunday
    Member
    Posted 3 years ago #

    Yes it is possible. You can use this action created_term or 'created_category'( assuming the taxonomy is category )

    heres a bit of code to get you started

    add_action('created_term', 'post_tax_to_feed', 3);
    function post_tax_to_feed( $term_id, $tt_id, $tax ){
    if( $tax == 'company' ){
    //its our custom taxonomy, insert the post here using wp_insert_post
    }
    }

    for inserting post function, see this page http://codex.wordpress.org/Function_Reference/wp_insert_post

  3. Blandanomics
    Member
    Posted 3 years ago #

    This looks great thanks for your help. I think I get how to use wp_insert_post, but do I put the code in the loop?

  4. Blandanomics
    Member
    Posted 3 years ago #

    nevermind, i see this goes into the functions file. Thanks for all your help. I'll have a hell of time playing with this.

    THANK YOU!

  5. Blandanomics
    Member
    Posted 3 years ago #

    So, I spent a night messing with this. I have the following code in my functions.php file:

    <?php
    
    add_action('created_term', 'post_tax_to_feed', 3);
    function post_tax_to_feed( $term_id, $tt_id, $tax ){
    if( $tax == 'company' ){
    
    // Create post object
      $my_post = array(
         'post_title' => 'My post',
         'post_content' => 'This is my post.',
         'post_status' => 'publish',
         'post_author' => 1,
      );
    
    // Insert the post into the database
      wp_insert_post( $my_post );
    
    }
    }
    
    ?>

    Whenever I create a new "company" I get the following error:

    http://www.flickr.com/photos/technicallyphl/5275229736/

    After refreshing the page the taxonomy gets created but no post is made.

    Any ideas on what's wrong?

  6. Reuben Gunday
    Member
    Posted 3 years ago #

    oops..something wrong the code I posted
    change this line

    add_action('created_term', 'post_tax_to_feed', 3);

    to

    add_action('created_term', 'post_tax_to_feed', 10, 3);

    Also, you can use this action too 'created_company'. In this case no need to check for $tax == and no need of those 2 extra arguments in add_action.

  7. Blandanomics
    Member
    Posted 3 years ago #

    Works like a charm. Thank you very much!

Topic Closed

This topic has been closed to new replies.

About this Topic