0

I have some code that I am using with Advanced Custom Fields, I have created a Featured post type and within that there is a dropdown to select Posts. The code (between the lines) is then suppose to pull back information about the post of selected post, not the featured post itself. This works well on a page, but when I have tried to move it to a widget it return the title, price, excerpt from the featured post rather than the post selected in the featured post.

I have tried to debug it with little success, I put a var_dump($post_object) just before the output and that showed the right information so am a little confused as to why it is not working. Any insight would be helpful, and if you think this is more about the ACF plugin rather than WordPress please let me know.

<?php

add_action( 'widgets_init', function(){
     register_widget( 'Featured_Property_Widget' );
}); 



/**
 * Adds Featured Property Widget.
 */
class Featured_Property_Widget extends WP_Widget {
    /**
     * Register widget with WordPress.
     */
    function __construct() {
        parent::__construct(
            'Featured_Property_Widget', // Base ID
            __('RealEstatePro - Featured Property', 'text_domain'), // Name
            array( 'description' => __( 'Add a featured property widget', 'text_domain' ), ) // Args
        );
    }
    /**
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget( $args, $instance ) {
?>

    <div class="col-xs-12 col-sm-12 col-md-12">

        <?php

        $sale_properties = array (
            'post_type'      => 'featured',
            'posts_per_page' => 1,
        );

        $featured_buys = new WP_Query( $sale_properties );

        while ( $featured_buys->have_posts() ) : $featured_buys->the_post(); 

            $post_object = get_field('featured_property');
                if( $post_object ): 

                // override $post
                $post = $post_object;
                setup_postdata( $post ); 

                ?>

                    <h4>Featured Property</h4>

                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><img src="<?php the_field('property_image_1');?>"></a>
                    <h5><span><?php the_title(); ?></span></h5>             

                    <?php if (get_field('sale_or_rent')=='sale' ) { ?>

                        <h4><?php echo $GLOBALS['theme_settings']['currency'].number_format(get_field('property_price'), 0); ?></h4>

                    <?php } else { ?>

                        <h4><?php echo $GLOBALS['theme_settings']['currency'].number_format(get_field('property_price'), 0).$GLOBALS['theme_settings']['rental_period']; ?></h4>

                    <?php } ?>

                    <h4><?php the_field('sub_title'); ?></h4>
                    <p><?php echo substr(get_field('property_details_excerpt'),0,90); ?>...<?php echo sprintf('<a href="%s">More details', esc_url( get_permalink() )),'</a>';?></p>


        </div>

        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
        <?php endif;

    endwhile; 

    wp_reset_query(); ?>

<?php
        echo $args['after_widget'];
    }


    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     */
    public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }
        else {
            $title = __( 'Featured Property', 'text_domain' );
        }
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
            <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
        </p>
        <?php 
    }


    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     */
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        return $instance;
    }
} // class Featured_Property_Widget
5
  • Why are you running a query, updating the $post object, then clobbering it? That doesn't make sense.
    – s_ha_dum
    Commented Jan 4, 2016 at 15:23
  • @PieterGoosen Sorry I had trouble getting it on here with the correct formating.
    – Naz
    Commented Jan 4, 2016 at 16:23
  • @s_ha_dum TBH I just took it from the WP codex and ACF instructions,what would be the correct way to do it?
    – Naz
    Commented Jan 4, 2016 at 16:25
  • I don't know exactly what you are doing yet. The code doesn't make sense so I can't infer the problem or the fix. Explain what you want in detail and in English.
    – s_ha_dum
    Commented Jan 4, 2016 at 17:58
  • @s_ha_dum I have a site that lists properties, using the standard wordpress loop, the properties have their own post type Properties. I also want to display featured properties throughout the site. So I create a new post type called Featured within that each post has a title and dropdown to select the property to feature. The dropdown is the ACF Object type. So the code is suppose to display the details of the post that was selected, but instead it is showing info of the featured post itself.
    – Naz
    Commented Jan 4, 2016 at 18:03

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.