WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;



share|improve this question
something is obviously overwriting the global $post variable. without seeing any code it's just guesses. – Milo Jan 3 '12 at 19:14
up vote 0 down vote accepted

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

share|improve this answer

Your Answer


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.