Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Chittaranjan Dec 18 '13 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. –  Chittaranjan Dec 18 '13 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 '13 at 6:27
    
because i want to organize them according to the number of the value of _wti_like_count <= 5 –  Oscar Dec 18 '13 at 6:29
    
That's fine. Please check my answer. –  Chittaranjan Dec 18 '13 at 6:33

2 Answers 2

up vote 2 down vote accepted

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 );
}
share|improve this answer
    
Thank you so much!! I appreciate it! –  Oscar Dec 18 '13 at 6:38
    
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 '13 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. –  Chittaranjan Dec 18 '13 at 7:05
    
Alright my friend! Thank you so much! You are a life saver :D –  Oscar Dec 18 '13 at 14:44

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;
share|improve this answer
    
post_author is not the required one, it will still create the post :) –  Sabita Sahoo Dec 18 '13 at 6:13
    
Yeah, i think is the way i query the my posts –  Oscar Dec 18 '13 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 '13 at 8:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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