Support » Plugins » Hacks » 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),
    	$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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hey Godthor,

    Nothing about that function included should trigger an infinite loop, unless I am missing something. Have you added this function as an action ( with add_action(); ) somewhere else in your plugin?

    For clarification wp_insert_post() does fire the save_post action. This is one way you could potentially create an infinite loop, if you have a function hooked to save_post that calls to wp_insert_posts().

    Hmmm, very odd. I’m not using add_action at all on save_post.

    Maybe I could explain a bit more and something may become obvious.

    I have a class for my plugin and in this case, when I need to use wp_insert_post, it’s on a page that I have a shortcode for. So, just to give only the relevant elements:

    if(!class_exists("cmdctr")) {
    	class cmdctr {
    		function __construct() {
    			add_shortcode('cmd_roster', array($this,'roster'));
    		function roster($atts, $content = null) {
    			global $FORCE_ORG, $_POST, $_GET;
    			require 'roster/functions.php';
    			if($_POST['done']) { // Saving it or something
    				require 'roster/save.php';
    				if($_POST['share'] || $_POST['review']) {
    					$roster_id = $_POST['roster_id'];
    					$the_post_id = get_value("SELECT post_type_id FROM " . T_ROSTERS . " WHERE id = '" . $roster_id . "'");
    					if($the_post_id) {
    					else {

    And of course my insert_post_type function is mentioned above.

    Now, at one point I had the code for inserting the post or updating it inside that save.php that gets included. At that point I was testing and the code for the inserts and updates was not in a function and I had no issues with an infinite loop. I then created the functions for insert & update and had those inside save.php and suddenly the looping began. I’ve since moved those functions out to my class as I was hoping it would help.

    For the hell of it I moved my insert and update back to my save.php file as I had it before when it worked without an infinite loop and no luck, still looping.

    There obviously must be something I’m overlooking that’s causing this loop. Without the insert or update functions in there things are fine. Meaning, my code on its own is not causing the loop. That being said, that’s not to say something in my code isn’t causing it once I put the insert or update functions in there but for the life of me I can’t figure out what. I’m not using add_action, filters, anything on save_post or wp_insert_post. I just can’t figure it out.

    Also, worth noting. As you’d guess, the save.php file saves the roster the user submitted. If I have insert or update post in my code then it appears my entire roster function shown above gets looped as a result. Not only does it just keep inserting posts, it also keeps saving the roster infinitely along with it. Again, if I remove wp_insert_post the roster is only saved/inserted once as it should be.

    I’ve finally figured it out!

    I had a plugin installed called Relevanssi that overrides the default search handler. Once I disabled it things worked perfectly. I know that plugin indexes the site and I can only assume it must have an add_action in there for save_post or wp_insert_post that was causing my infinite loop.

    Maybe this will help someone else out in the future.

    Oops, forgot to mark this as resolved.

    hi godthor,

    did you get your post module/plugin working as im interested in doing something similar for a project im working on. Would be good to talk to you

    Thanks !

    Indeed I did. It was really simple once I figured out that another plugin was causing all my issues.

    im not sure if what you did is what I need now….. i would like to have the ability to generate a post with a list of parameters that i can either check in an automated script to make sure its not spam or effectively a guest post function with a limited set of parameters (title, content, tags) that can be a form on my wordpress site on a specific page/post.

    sort of like a contact form but if the parameters are accepted then it generates a post with the parameters entered, a category i have hardcoded, and some additional hardcoded text added as well

    Had the same infinite loop issue occurring in the form passed through the shortcode on the page with Relevanssi active. Thanks a lot, godthor!
    Saved me hours of debugging.
    Also, in case someone has the same issue and wants to use Relevanssi as well, the plugin has an option to exclude posts/pages from indexing. If you pass the page id that has the shortcode in question, it won’t index it and won’t cause an infinite loop.
    Hope it helps someone.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘wp_insert_post & wp_update_post’ is closed to new replies.