0

I am creating a widget that displays data from my post type 'testimonials'. The back end of the widget is working fine and displays a list of testimonials and allows me to select which I want to appear in the front end.

However my WP_Query doesn't seem to be pulling in any data at all.

Here is the code for the front end of my widget.

    public function widget( $args, $instance ) {
        // extract the widget arguments
        extract( $args );
        $testimonial = $instance['testimonials'];
        $options = $instance['options'];

        // If All option selected display all testimomials
        if ( $options == 'All' ) {
        $args = array ( 'post-type' => 'testimonials',
                        'post_status' => 'publish',
                        'posts_per_page' => 20,
                        'orderby' => 'title',
                        'order' => 'ASC' 
            );
        // else only display the selected testimonial
        } else {
            $args = array( 'post-type' => 'testimonials',
                            'post-status' => 'publish',
                            'post_per_page' => 20,
                            'orderby' => 'title',
                            'order' => 'ASC',
                            'post__in' => $testimonial );
        }

        $my_query = new WP_Query( $args );


?>


    <div id="rotate">
        <!-- WordPress Loop -->
        <?php if ( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post() ?>
                <p> <!-- echo the testimonial content, trim white space and strip out any unwanted html tags -->
                    "<?php echo trim( strip_tags( get_the_content() ) ); ?>"<br/>
                    <!-- print out the company name -->
                    <span><?php the_title(); ?></span>
                </p>

        <?php endwhile; endif;
        // reset WP query, restores $wp_query and global post data to original main query  
        wp_reset_query(); ?>
    </div>

<?php   }

And the code I use to register the post type

register_post_type( 'testimonials',
    array(
        'labels' => array(
            'name' => __( 'Testimonials' ),
            'singular' => __( 'Testimonial' ),
            'add_new_item' => __( 'Add New Testimonial' ),
            'edit_item' => __( 'Edit Testimonial' )
            ),
        'public' => true,
        'has_archive' => true,
        'menu_position' => 20,
        'menu_icon' => 'dashicons-groups',
        'supports' => array( 'title','editor')
        )
    );

Can anyone see where I am going wrong? Any help would be appreciated.

1 Answer 1

0

The syntax is not 'post-type' or 'post-status', it is with an underscore instead of a dash:

'post_type' => 'testimonials',
'post_status' => 'publish',

Your Answer

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

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