0

I dynamically create iterations of a form field with jquery, but they all carry the name extrakosten[] for the first variable and the other variable vergoeding[].

The php:

$postTitle = ''; // save post title

if(isset(   $_POST['submit']    )){             // if submit button is present

    for ($i=0; $i < count($_POST['vergoeding']); $i++) {

        $new_post = array(
            'post_title'    =>  $postTitle,
            'post_content'  =>  ' ',                        // can not be empty
            'post_status'   =>  'publish',          
            'post_type'     =>  'landschapselement'         // custom post type
        );
        $pid = wp_insert_post($new_post);                   // insert the post

        wp_set_post_terms( $pid, $_POST['extrakosten'],'kostenpost',false);
        wp_set_post_terms( $pid, $_POST['vergoeding'],'vergoedingaanleg',false);

} // close for loop

}

What happens now, is that when I click the submit button, a new post under custom post type 'landschapspakket' is made. The values under vergoeding and kostenpost are being posted in the custom taxonomy 'vergoedingaanleg' and 'kostenpost'.

What it does now, is create ONE post with TWO values for 'kostenpost' and 'vergoeding'. What I try to achieve is to create TWO posts, each with ONE value for both 'kostenpost' and 'vergoeding'.

I think something might be wrong with the array. I'm new to arrays. Any suggestions? Thanks for the help

0

You could try something like this:

$postTitle = ''; // save post title

if(isset( $_POST['submit'])){             // if submit button is present

    $vergoeding=$_POST['vergoeding'];
    $extrakosten=$_POST['extrakosten'];

    for ($i=0; $i < count($vergoeding); $i++) {

        $new_post = array(
            'post_title'    =>  $postTitle,
            'post_content'  =>  ' ',                        // can not be empty
            'post_status'   =>  'publish',          
            'post_type'     =>  'landschapselement'         // custom post type
        );
        $pid = wp_insert_post($new_post);                   // insert the post

        if(isset($extrakosten[$i]) && $pid > 0){
            wp_set_post_terms( $pid, $extrakosten[$i],'kostenpost',false);
        }
        if(isset($vergoeding[$i]) && $pid > 0){
            wp_set_post_terms( $pid, $vergoeding[$i],'vergoedingaanleg',false);
        }

    } // close for loop

}

It looks like you where inserting an array of terms in each wp_set_post_terms(). I try to add a string instead in the above code example.

ps: It is not clear why you use count($vergoeding) if you want only two inserted posts? So it looks like this array is supposed to include 2 items.

  • The website is live at www.areal-agro.nl/tool/page_id=235. There you can see that the textfield vergoeding and the textfield kostenpost are duplicated in unison. I want to make a seperate post for each duplicate. How can I do that? – Lars van Peij Feb 15 '13 at 9:19
  • I don't really understand your code, but it works like a charm. Thanks @birgire! – Lars van Peij Feb 15 '13 at 10:31
  • ok great it worked out for you ;-) – birgire Feb 15 '13 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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