0

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();
}
5
  • Hi, why do you use global $post while creating a new loop?
    – JMau
    Mar 21, 2014 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. Mar 21, 2014 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, 2014 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. Mar 24, 2014 at 7:14
  • @shyammakwana.me I have the same issue. do you get any solutions? Jun 18, 2020 at 4:31

0

Your Answer

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

Browse other questions tagged or ask your own question.