2

I have a front end form that let users submit a post.

This is how i store the data when a post is submitted :

if ( isset( $_POST['submitted'] )) {
        $post_information = array(
        'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
        'post_content' => $_POST['postContent'],
        'post_type' => 'post',
        'post_status' => 'publish'
    );
$new_post = wp_insert_post( $post_information );

The post is not showing in my post page unless i go through my dashboard and click over The UPDATE button.

THIS IS HOW I QUERY MY POSTS :

$args = array(
 'posts_per_page' => 5,
'paged' => $paged,
'meta_query' => array(
array( 'key' => '_wti_like_count','value' => 5, 'compare' => '<=','type' => 'numeric')
)
);

query_posts( $args );

How can i make my submitted posts publish automatically?

5
  • The above code works for me. Do you have any kind of approval system for the posts? In case the post's status is draft or pending review, you should see PUBLISH button. Since you see UPDATE button, the post must have been published. Dec 18, 2013 at 6:11
  • Are you sure when you save the post from frontend, the post has meta data _wti_like_count? If it does not have, then it will never show up that post. Try the above $args without the meta_query and see whether it shows up the post or not. Dec 18, 2013 at 6:23
  • i did it without the meta_query and they show up! how do i add the meta data _wti_like_count
    – Oscar
    Dec 18, 2013 at 6:27
  • because i want to organize them according to the number of the value of _wti_like_count <= 5
    – Oscar
    Dec 18, 2013 at 6:29
  • That's fine. Please check my answer. Dec 18, 2013 at 6:33

2 Answers 2

6

The post gets added and published but since you have the meta query and the meta key is not added when you submit the post from frontend, it does not show up. Use the following code which adds the meta data as needed.

if ( isset( $_POST['submitted'] ) ) {
     $post_information = array(
                              'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
                              'post_content' => $_POST['postContent'],
                              'post_type' => 'post',
                              'post_status' => 'publish'
                         );

     $new_post = wp_insert_post( $post_information );

     // Add the post meta
     add_post_meta( $new_post, '_wti_like_count', 0, true );
     add_post_meta( $new_post, '_wti_unlike_count', 0, true );
     add_post_meta( $new_post, '_wti_total_count', 0, true );
}
5
  • Another little thing my friend! if i add a value like this add_post_meta( $new_post, '_wti_like_count', 1, true ); why the value 1 doesn't display on the front end ? It only shows 0
    – Oscar
    Dec 18, 2013 at 6:58
  • The data you see on the frontend is pulled from the plugin table wti_like_post. The meta data is just added to the post so that it can be used in custom queries like the one you have above. To keep things clean, always 0 is added to the post when created. As user keeps on voting for that post, both the values ( in the plugin table and meta data ) gets updated accordingly. Dec 18, 2013 at 7:05
  • Alright my friend! Thank you so much! You are a life saver :D
    – Oscar
    Dec 18, 2013 at 14:44
  • Note that you can now do this within the wp_insert_post using the 'meta_input' attribute (e.g. meta_input => array('meta_field_name => 'meta field value'))
    – Julian K
    Nov 13, 2016 at 22:12
  • Hi all, Can you guys help me to solve the same issue? I am unable to solve the issue by going this way.
    – Jomol MJ
    Dec 26, 2017 at 12:28
0

I am not sure but i think that is because of you didn't added the author into the $post_information :

if( isset($_POST['submitted']) ):
    global $user_ID;
    $post_information = array(             
        'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
        'post_content' => $_POST['postContent'],
        'post_type' => 'post',
        'post_status' => 'publish', 
        'post_author' => $user_ID, 
        'post_date' => date('Y-m-d H:i:s')
    );
    $post_id = wp_insert_post($post_information);
    if (!$post_id) {
        wp_die('Error');
    }
endif;
3
  • post_author is not the required one, it will still create the post :) Dec 18, 2013 at 6:13
  • Yeah, i think is the way i query the my posts
    – Oscar
    Dec 18, 2013 at 6:16
  • @SabitaSahoo thanks to make me correct :) but the OP has not cleared his question as you can see that he updated his question.
    – jogesh_pi
    Dec 18, 2013 at 8:05

Your Answer

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

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