0

I have a custom post type called 'blog' . I wish to show a selected number of posts from this custom post type in a widget. But all I get is 'No listing found,' which is the else statements if no posts can be found. There are posts. What is wrong?

My custom post type:

function blog_post_type() {
    $labels = array(
        'name' => 'Blog',
        'singular_name' => 'Blog post',
        'add_new' => 'Add new blog post',
        'add_new_item' => 'Add new blog post',
        'edit_item' => 'Edit blog post',
        'new_item' => 'New blog post',
        'all_items' => 'All blog posts',
        'view_item' => 'View blog post',
        'search_items' => 'Search blog posts',
        'not_found' => 'No blog posts found',
        'not_found_in_trash' => 'No blog posts found in trash',
        'menu_name' => 'Blog posts',
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'supports' => array(
            'title',
            'editor',
            'author',
            'thumbnail',
            'excerpt',
            'post-formats' ),
        'has_archive' => true,
        'taxonomies' => array( 'post-tag' ),
        'show_in_menu' => true,
    );

    register_post_type( 'blog posts', $args );
}

add_action( 'init', 'blog_post_type' );

My widget:

class Blog_Widget extends WP_Widget {
    function __construct() {
        parent::__construct(
            'blog_widget', // base id
            'Blog Widget', // Name
            array(
                'description'=> __('Displays the latest post excerpt')
            )
        );
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['numberOfListings'] = strip_tags($new_instance['numberOfListings']);
        return $instance;
    }

    function form($instance) {
        if($instance) {
            $title = esc_attr($instance['title']);
            $numberOfListings = esc_attr($instance['numberOfListings']);
        }else {
            $title = '';
            $numberOfListings = '';
        }
        ?>
<p>
  <label for="<?php echo $this->get_field_id('title'); ?>"><?php  _e('Title', 'blog_widget'); ?> </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 $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('numberOfListings'); ?>"><?php _e('Number of Listings:', 'blog_widget'); ?> </label>
<select id="<?php echo $this->get_field_id('numberOfListings'); ?>" name="<?php echo $this->get_field_name('numberOfListings'); ?>">
<?php for($x=1; $x<=10; $x++): ?>
<option <?php echo $x == $numberOfListings ? 'selected="selected"' : ''; ?> value="<?php  echo $x; ?>"><?php echo $x; ?> </option>
<?php endfor; ?>
</select>
</p>
<?php 
    }


    function widget($args, $instance) {
        extract( $args ) ;
        $title = apply_filters('widget_title', $instance['title']);
        $numberOfListings  = $instance['numberOfListings'];
        echo $before_widget;
        if($title) {
            echo $before_title . $title . $after_title;
        }
        $this->getBlogExcerpt($numberOfListings);
        echo $after_widget;
    }

    function getBlogExcerpt($numberOfListings) { //html
        global $post;
        add_image_size( 'realty_widget_size', 85, 45, false );
        $listings = new WP_Query();
        $listings->query('post_type=blog&posts_per_page=' . $numberOfListings );  
        if($listings->found_posts > 0) {
            echo '<ul class="realty_widget">';
            while ($listings->have_posts()) {
                $listings->the_post();
                $image = (has_post_thumbnail($post->ID)) ? get_the_post_thumbnail($post->ID, 'realty_widget_size') : '<div class="noThumb"></div>'; 
                $listItem = '<li>' . $image; 
                $listItem .= '<a href="' . get_permalink() . '">';
                $listItem .= get_the_title() . '</a>';
                $listItem .= '<span>Added ' . get_the_date() . '</span></li>'; 
                echo $listItem; 
            }
            echo '</ul>';
            wp_reset_postdata(); 
        }else{
            echo '<p style="padding:25px;">No listing found</p>';
        } 
    }
}

What does it default to the else 'no listings found' ? There are posts available. Thank-you!

  • Any ideas, anyone please? It would be much appreciated! – Leona grey Mar 5 '15 at 15:24
  • You register your post type as blog posts, not blog. – TheDeadMedic Mar 5 '15 at 15:33
2

I don't know if it is a typo, but your custom post type name is incorrect, it cannot have spaces, and also you should just use underscores to separate names in custom post type names

register_post_type('blog posts', $args );

Is it just blog or blog_posts

In your query in your widget, you are querying the custom post type blog, which does not match your registered custom post type.

Last note, you should not use extract() at all. Look at the Widget API on how to coreectly construct a widget

  • Thanks, it was a typo. I will read up about `extract. Thank-you for the help! – Leona grey Mar 5 '15 at 16:00

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.