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 following code in my functions.php. When I try to add new post_type (e.g. post,lawyers) it fills excerpt,slug (this affects in permalink structure), other metaboxes from first post of lawyers post type.

I have googled but found something that says adding global $post; will solve my problem. But still problem exists.

function lawyers_checkbox_display( $post ) {
    global $post ; 
    wp_nonce_field( 'lawyers_checkbox_display', 'lawyers_checkbox_display_nonce' );


    $data  = "";
    if ($post->filter != 'raw') {
            $data = get_post_meta($post->ID,'_lawyer_id',true);
    }

    $args = array(
            'posts_per_page' => -1,
            'post_type' => 'lawyers',
            'post_status' => 'publish',
            );
            $query_lawyers = null;
        $query_lawyers = new WP_Query($args);
        while($query_lawyers->have_posts()):
            $query_lawyers->the_post();

                   $select = @in_array($post->ID,$data) ? 'checked="checked"' : '';     
                    echo '<input type="checkbox" value="'.$post->ID.'" '.$select.' id="'.$post->ID.'" name="lawyer_id[]" />';
                    echo '<label for="'.$post->ID.'">';
                            _e( the_title(), 'myplugin_textdomain' );
                    echo '</label> ';
                    echo '<br/>';
        endwhile; 
            wp_reset_postdata();
}

I have found solution by using get_posts() in my function.

Can any one explain what the difference it makes ? Why WP_Query is not working properly.

    function lawyers_checkbox_display( $post ) {
    global $post ; 
    wp_nonce_field( 'lawyers_checkbox_display', 'lawyers_checkbox_display_nonce' );


    $data  = "";
    if ($post->filter != 'raw') {
            $data = get_post_meta($post->ID,'_lawyer_id',true);
    }

    $args = array(
            'posts_per_page' => -1,
            'post_type' => 'lawyers',
            'post_status' => 'publish',
            );

            $myposts = get_posts( $args );
            foreach ( $myposts as $post ) {
              setup_postdata( $post );
                   $select = @in_array($post->ID,$data) ? 'checked="checked"' : ''; 

                    echo '<input type="checkbox" value="'.$post->ID.'" '.$select.' id="'.$post->ID.'" name="lawyer_id[]" />';
                    echo '<label for="'.$post->ID.'">';
                            _e( $post->post_title, 'myplugin_textdomain' );
                    echo '</label> ';
                    echo '<br/>';
            }
            wp_reset_postdata();
}
share|improve this question
    
Hi, why do you use global $post while creating a new loop? –  JMau Mar 21 at 7:11
    
I googled and there was answer that says by adding global $post it will work. but it's not working with/without it. –  TechnoKnol Mar 21 at 9:10
    
What exactly happens differently with WP_Query? Since get_posts() is just a thin wrapper around it, they can almost always be used absolutely interchangeably. –  Rarst Mar 23 at 19:22
    
even I'm thinking why it happens? Exactly it overrides slug,excerpt,custom fields,permalink from first post of custom_post_type(e.g. lawyers) in short it auto fills all those fields. –  TechnoKnol Mar 24 at 7:14

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.