0

Much like this question, I'm having trouble with wp_insert_post causing recursive post adding. I do, however, have implemented a page type check - the problem is the inserted post always gets added as the same type, no matter the type specified.

function create_auto_post($post_ID) {
    if (get_post_type($post_ID) != 'manually-published') return;

    $post_ID = wp_insert_post(
            array(
                'post_status'       =>  'publish',
                'post_type'         =>  'automatically-published'
            )
        );
}

add_filter( 'save_post', 'create_auto_post', 1, 1);

The inserted post is of type manually-published, therefore triggering a recursive insertion of post until database time out. What am I doing wrong?

Note: I have tried using the 'publish_manually-published' action for the filter, the problem is the same - post gets added as 'manually-published' instead of 'automatically-published'.

closed as unclear what you're asking by Tom J Nowell Jan 19 '18 at 12:09

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • is this your full function code? – inarilo Dec 18 '17 at 21:32
  • @inarilo it's not the full planned code, but it's where i am getting stuck already. – codesquid Dec 18 '17 at 22:10
  • maybe it's because you are using add_filter instead of add_action? your code looks fine to me. – inarilo Dec 18 '17 at 22:17
  • a really fair point and one i can't believe i missed. will test and report if it works, if yes please submit as answer. – codesquid Dec 18 '17 at 22:20
1

The problem is that your code creates a post if the created post is not manually-published, but the new post is automatically-published, and those don't match. Your conditional is not explicit enough and eneds to skip the scenario when your inserted post is inserted to prevent an infinite loop

Instead of:

function create_auto_post($post_ID) {
    if (get_post_type($post_ID) != 'manually-published') return;

Consider:

function create_auto_post( $post_ID, $post ) {
    if ( in_array( $post->post_type, [ 'manually-published', 'automatically-published' ] ) {
        return;
    }

Further notes:

  • This isn't a filter it's actually an action, use add_action instead
  • The save_post action passes the post object as a second parameter, it's better to use that
  • You'll need to indicate that this function takes 2 not 1 parameter for this to work
  • Your original code assumes all posts will be of 2 post types, but you forget that images/attachments are also a post type, as are menu items in nav menus, and many other things that other plugins add. Right now it's going to create automatically-published posts for all your menu items and attachments, as well as other things you might not have considered
  • hi, thanks for the help! i have some questions: 1. what's the difference between get_post_type($post_ID) and $post->post_type? is $post_ID not the ID of $post? why is using $post better? the answer in the question i linked states that the $post object is not updated and specifically recommends using $post_ID in its stead. 2. if you check closer, you'll see i am returning the function for posts that are NOT of type 'manually-published', i.e. the following code should only get executed for posts of type 'manually-published'. am i missing something here? – codesquid Dec 18 '17 at 22:09
  • 1. they do the same thing, but the one I used is more direct, and I prefer to be explicit, afterall get_post_type is doing the same thing internally so why not cut out the middle man? But if you prefer the other method feel free – Tom J Nowell Dec 18 '17 at 23:15
  • 2. Sure, but that's a very wide net you're casting, now automatically published posts are going to get created for everything, including automatically created posts, which will trigger the hook, which will create an automatically published post, which will trigger the hook, which will create an automatic... etc etc so automatically-published needs to be excluded. I do have to wonder why this is all being done though, at a minimum the number of posts is doubled – Tom J Nowell Dec 18 '17 at 23:17
  • 1. thanks for the clarification! 2. the post types provided are placeholders. in practice, i want to create new posts only when a certain type of post is created, and even then with additional checks in place. the 'manually-created' post type here is not equivalent to the 'post' type. – codesquid Jan 17 '18 at 5:40
  • that complicates things, now it's impossible to know the actual flow of things and diagnose because you've hidden the post types you're actually using. Eitherway the crux of the problem is that wp_insert_post fires the save_post hook, and you're calling wp_insert_post on that hook, creating an infinite loop – Tom J Nowell Jan 17 '18 at 14:52

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