• Resolved blandanomics

    (@blandanomics)


    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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 https://codex.www.ads-software.com/Function_Reference/wp_insert_post

    Thread Starter blandanomics

    (@blandanomics)

    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?

    Thread Starter blandanomics

    (@blandanomics)

    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!

    Thread Starter blandanomics

    (@blandanomics)

    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:

    https://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?

    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.

    Thread Starter blandanomics

    (@blandanomics)

    Works like a charm. Thank you very much!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘A WordPress "news feed"’ is closed to new replies.