Support » Developing with WordPress » Foreach wp_insert_post not working

  • Hello,

    I’m sorry, I speak a little English.

    My custom plugin:

    ~/wp-content/plugins/mypluk/mypluk.php

    <?php
    
    /**
     * Plugin Name: mypluk
     */
    
    	function videoss() {
    
    		$postarr = array();
    
    		foreach( array( 1, 2, 3 ) as $key ) {
    
    			$postarr [ $key ] = array(
    
    				'post_title'=> 'title' . $key
    
    			);
    
    			wp_insert_post( $postarr [ $key ], $wp_error );
    
    		}
    
    	}
    
    	videoss();
    
    ?>

    It only adds 1 post: title1 (post_title). After, I see: screenshot

    Why?

    Please help me. Thanks.

    • This topic was modified 1 month, 2 weeks ago by otto2021.
    • This topic was modified 1 month, 2 weeks ago by otto2021.
    • This topic was modified 1 month, 2 weeks ago by otto2021.
    • This topic was modified 1 month, 2 weeks ago by otto2021.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Testing this locally, I found that the error is Call to undefined function is_user_logged_in(). This means that wp_insert_post is being called too early – before that function has been loaded.

    You should run that when an appropriate hook is run. Just be aware that every time the hook is run, it will insert posts to your site.

    I would recommend setting up a local development environment with debugging set up to allow you to to see any errors that are being fired.

    Best of luck!

    Thread Starter otto2021

    (@otto2021)

    Dear Ryan Welcher,

    How to create the hook? I am beginner. Thanks.

    I would recommend reading the Hooks overview here. This page will explain the differences between the two type of Hooks ( actions and filters ) and also give some examples.

    For your case, instead of directly calling the videoss() function, you would do something like the following:

    /**
     * Plugin Name: mypluk
     */
    
    	function videoss() {
    
    		$postarr = array();
    
    		foreach( array( 1, 2, 3 ) as $key ) {
    
    			$postarr [ $key ] = array(
    
    				'post_title'=> 'title' . $key
    
    			);
    
    			wp_insert_post( $postarr [ $key ], $wp_error );
    
    		}
    
    	}
    
        add_action( 'init', 'videoss' );
    
    ?>

    Please keep in mind that Hooks are run on every page load and by extension so will your code. That means you’ll be adding new posts every time you load a new page.

    • This reply was modified 1 month, 2 weeks ago by Ryan Welcher.
    Thread Starter otto2021

    (@otto2021)

    Dear Ryan Welcher,

    Thank you very much. It works. My new question:

    // before:
    function videoss( $url, $tag ) {}
    videoss( 'https://*', '*' );
    // now:
    add_action( 'init', 'videoss', 'https://*', '*' );

    How to add function args to add_action?

    Moderator bcworkz

    (@bcworkz)

    You don’t! The args passed to your callback function are determined by the do_action() code. Refer to the add_action() docs page. You have the first two correct. The third should be an integer. 10 is usually fine. The fourth is how many args you are collecting. In your example, this should be 2, however, if we look at the do_action('init') call in core code, we can see that no args are passed with this action, so there is no point in even providing a fourth arg to add_action(). You cannot collect any args with this action.

    If you need data that is not within scope of the callback, you’ll either need to declare the variables global, or assign the data to static class properties.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.