wp_insert_post & wp_update_post
-
I’ve built a plugin and decided to try and use custom post types. I have those setup fine, however, the problem I’m running into is using wp_insert_post and wp_update_post. People who join the site are not created as WP users and in turn do not log into the site via-WP. Instead it’s a user system I created and they authenticate through that. The reason being that I want them to interface with my plugin from the site, not from the WP user/admin area. That’s all fine too.
Now, what they are doing is creating a roster sheet for a game. What I’ve been trying to do is when they save their roster it creates a post under my custom post type. When they update said roster it updates the post.
I did have this working, so I thought, and then a little while later I encountered the infinite loop issue. I’ve seen how others have avoided this issue but the difference with me and them is I’m not using the GUI in WP to work with the post type and thus I’m not firing the save_post action, well, at least directly. What I’ve seen those users do is add_action for save_post and then in their function remove_action, do wp_insert_post and re-add the action back. That fixes their infinite loop problem.
Sorry, this is getting long but I want to be clear. So, what I’d like to do is use wp_insert_post and wp_update_post from outside the admin/user area and not get stuck in an infinite loop.
Basically the code I have now is something like this:
function insert_post_type($roster_id) { $sql = new conn(); $r_name = get_value("SELECT name FROM " . T_ROSTERS . " WHERE id = '" . $roster_id . "'"); $my_post = array( 'post_title' => $roster_id, 'post_content' => '[cmd_list id=' . $roster_id . ']', 'post_excerpt'=>$r_name . ' is a Warhammer 40K list created by ' . get_value("SELECT name FROM " . T_USERS . " WHERE id = '" . THIS_USER . "'") . ' using Command Center', 'post_status' => 'publish', 'post_author' => 1, 'post_category' => array(1), 'post_type'=>'cmd_lists' ); $the_post_id = wp_insert_post( $my_post ); $sql->conn("UPDATE " . T_ROSTERS . " SET post_type_id = '" . $the_post_id . "' WHERE id = '" . $roster_id . "'"); }
You can see I’m using my own connection class and tools but those aren’t the issue of course. Once I insert the post I then update their roster with that post ID for later use on the front end.
For the record, security should not be an issue. I’ve made sure to utilize nonces and I do my own security checks as well.
Any help would be appreciated.
- The topic ‘wp_insert_post & wp_update_post’ is closed to new replies.