1

I have a function to insert a single post in the database.

But when I run the function, duplicate posts (40) are added to the database and then an error is displayed.

Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\wamp\www\wordpress\wp-includes\cache.php on line 671

I would like it so that each time the function is run only one post is saved, without duplicates.

function ghoolo_mango(){

    $oiobz1 = writeMsg(1, 'center', '', 1);
    $postdate =date('Y-m-d');
    $ta =date('Y-m-d');
    $postdate_gmt =date('Y-m-d H:i:s');
    $titles='زندگی'.$postdate;
    $posts = array(
        'post_content'   => $oiobz1,
        'post_name'      => $titles,
        'post_title'     => $titles,
        'post_status'    => 'publish',
        'post_type'      => 'post',
        'post_author'    => '1',
        'ping_status'    => 'publish',
        'to_ping'        => 'http://rpc.pingomatic.com/',
        'post_date_gmt'  => $postdate_gmt,
        'post_date'  => $postdate,
        'tags_input'     => ", قیمت طلا در امروز $ta, قیمت طلا در امروز ,قیمت امروز طلا $ta,قیمت طلا $ta , قیمت امروز طلا $ta, قیمت طلا ماه $ta,لیست امروز قیمت طلا $ta,دانلود لیست قیمت طلا $ta,$ta قیمت طلا به روز,قیمت روز طلا,قیمت ",
    );

    $post_id = wp_insert_post($posts);
    ghoolo_mango();

}
add_action('wp', 'ghoolo_mango');

1 Answer 1

3

Looking at your code you are calling the function ghoolo_mango() by using the the wp actoin hook. This is fine but be aware that it will add your post everytime a page loads.

The problem is that you are also calling the function ghoolo_mango() within the function itself - i.e. it will keep calling it over and over.

To fix the problem, remove the line ghoolo_mango(); at the end of the function.

Your code should look like this -

function ghoolo_mango(){

    $oiobz1 = writeMsg(1, 'center', '', 1);
    $postdate =date('Y-m-d');
    $ta =date('Y-m-d');
    $postdate_gmt =date('Y-m-d H:i:s');
    $titles='زندگی'.$postdate;
    $args = array(
        'post_content'   => $oiobz1,
        'post_name'      => $titles,
        'post_title'     => $titles,
        'post_status'    => 'publish',
        'post_type'      => 'post',
        'post_author'    => '1',
        'ping_status'    => 'open',
        'to_ping'        => 'http://rpc.pingomatic.com/',
        'post_date_gmt'  => $postdate_gmt,
        'post_date'      => $postdate,
        'tags_input'     => ", قیمت طلا در امروز $ta, قیمت طلا در امروز ,قیمت امروز طلا $ta,قیمت طلا $ta , قیمت امروز طلا $ta, قیمت طلا ماه $ta,لیست امروز قیمت طلا $ta,دانلود لیست قیمت طلا $ta,$ta قیمت طلا به روز,قیمت روز طلا,قیمت ",
    );
    
    $post_id = wp_insert_post($args);
    
}
add_action('wp', 'ghoolo_mango');

Edit

You have now also asked how to insert future posts.

To do this, all you have to do is change the post date to a future date and change the post status to future. Because you include the date in your title, you'll also need to update that, but it should be as simple as this -

$postdate = new DateTime(date('Y-m-d H:i:s') . ' + 1 day');
$postdate_gmt = new DateTime(gmdate('Y-m-d H:i:s') . ' + 1 day');
$titles='زندگی' . $postdate->format('Y-m-d');
$args = array(
    { other args... }
    'post_status' => 'future',
    { other args... }
);

In the example above I've used 1 day, but you can alter that as required.

3
  • 1
    Glad to help :)
    – David Gard
    Commented Feb 12, 2015 at 15:30
  • I have Another question ... In The top code // 'ping_status' => 'publish' But I don't know When save in the database not publishing rather futuring .. @David Gard
    – Mortzea
    Commented Feb 12, 2015 at 16:50
  • I've updated my answer. I also noticed that in the original answer pint_status was set to publish - this can only be open or closed, so make sure you update your code accordingly.
    – David Gard
    Commented Feb 13, 2015 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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