0

I am currently creating a post programmatically, with the wp_insert_post function but am having some difficulties when adding two variables into the content part of the array. I would like to add $_POST['poll-description'] as well as the code I am currently using '[poll id=' . $latest_pollid . ']' to be the post content.

Is there any way I can use both of these in the following code?

$post_id = wp_insert_post(
    array(
        'comment_status'    =>  'open',
        'ping_status'       =>  'closed',
        'post_author'       =>  $current_user->ID,
        'post_name'         =>  $slug,
        'post_title'        =>  $pollq_question,
        'post_status'       =>  'publish',
        'post_type'         =>  'post',
        'post_content'      =>  '[poll id=' . $latest_pollid . ']'
    )
);

Would this work?

`post_content'  =>  $_POST['poll-description'] '[poll id=' . $latest_pollid . ']'`
2
  • what is $latest_pollid in your case. Where are you getting it. are you trying to insert a shortcode in post description along with content?
    – WPTC-Troop
    Commented Dec 24, 2015 at 10:17
  • @WPTC-Troop latest poll id returns a number which yes makes up a shortcode in the content. I would like the content from $_POST['poll-description'] to go before the shortcode.
    – dwinnbrown
    Commented Dec 24, 2015 at 10:19

2 Answers 2

0
$post_id = wp_insert_post(
    array(
        'comment_status'    =>  'open',
        'ping_status'       =>  'closed',
        'post_author'       =>  $current_user->ID,
        'post_name'         =>  $slug,
        'post_title'        =>  $pollq_question,
        'post_status'       =>  'publish',
        'post_type'         =>  'post',
        'post_content'      =>  $_POST['poll-description'] . '[poll id=' . $latest_pollid . ']'
    )
);
0

Just combine(concatinate) posted description and your shortcode and then inser it in post.

$concat_content = $_POST['poll-description'].'[poll id="' . $latest_pollid . '"]';

$post_id = wp_insert_post(
    array(
        'comment_status'    =>  'open',
        'ping_status'       =>  'closed',
        'post_author'       =>  $current_user->ID,
        'post_name'         =>  $slug,
        'post_title'        =>  $pollq_question,
        'post_status'       =>  'publish',
        'post_type'         =>  'post',
        'post_content'      =>  $concat_content
    )
);

If you find the above concatination confusing try the below

$concat_content = $_POST['poll-description'];
$concat_content .= '[poll id="' . $latest_pollid . '"]';

you can even insert a break tag between content and shortcode

2
  • I appreciate your answer but Omar's (above) seemed easier to implement so I chose his as correct.
    – dwinnbrown
    Commented Dec 24, 2015 at 10:46
  • oh ok. Jsut for your understanding I concatinated first.
    – WPTC-Troop
    Commented Dec 24, 2015 at 10:55

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.