There is something weird happening in a recent theme I have developed.
Inside the post page when I use $post->ID and get_the_ID() it always returns the same ID which belongs to one of my posts!
I'm wondering what's happening there?!!!

Edit: I found the reason behind this behavior. And it is that I have created a widget which inside it I use the WP_Query class (you can see the code at the end) and I guess it's overwriting the global $post.

class TextAds extends WP_Widget{

function TextAds(){

    $widget_ops = array( 'description' => __( "نمایش تبلیغات متنی", 'appthemes') );

    $this->WP_Widget('textads', __('تبلیغات متنی', 'appthemes'), $widget_ops);


function widget($args, $instance){

    //global $userdata;


    if ( !empty($instance['title']) ) {

        $title = $instance['title'];

    } else {

        $title = 'تبلیغات متنی';


    echo $before_widget;

    $title = apply_filters('widget_title', $title, $instance, $this->id_base);

    $term = get_term_by( 'slug','text', 'ad_cat' );

    $text_query = new WP_Query( array('post_type' => APP_POST_TYPE, APP_TAX_CAT => $term->slug, 'ignore_sticky_posts' => 1 ));


                    <h2 class="widgettitle"><?php echo $title; ?></h2>

                    <div class="clr"></div>

                    <?php while ( $text_query->have_posts() ) : $text_query->the_post(); ?>

                        <div class="ad0text"><a href="<?php

                        //$url = get_post_meta(get_the_ID(),'cp_url',true);

                        if(stripos($url,'http://') === false){

                            $url = 'http://'.$url;


                         echo $url; ?>"><?php the_title(); ?></a></div>

                        <div class="divider"></div>

                    <?php endwhile; ?>


    echo $after_widget;


function form( $instance ) {


        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('عنوان ابزارک:', 'appthemes') ?></label>

        <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php if (isset ( $instance['title'])) {echo esc_attr( $instance['title'] );} ?>" /></p>



    function update( $new_instance, $old_instance ) {

        $instance['title'] = strip_tags(stripslashes($new_instance['title']));

        return $instance;



  • 1
    something is obviously overwriting the global $post variable. without seeing any code it's just guesses. – Milo Jan 3 '12 at 19:14

Ah shame...
I should've used wp_reset_postdata() at the end of the loop.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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