WordPress.org

Ready to get started?Download WordPress

Forums

WordPress Popular Posts
Filter by Custom Post Types (18 posts)

  1. learnthat
    Member
    Posted 4 years ago #

    Hi Ikki,

    I am working on a project with several custom post types and want to create widgets which filter to different custom post types. Is this a planned feature?

    Thanks!
    Jeremy

  2. Héctor Cabrera
    Member
    Plugin Author

    Posted 4 years ago #

    Hi Jeremy,

    Haven't thought of that yet, could be a good idea. Gotta say that I haven't explored WordPress 3.0 thoroughly yet, some new features like the custom post type are completely unknown to me.

    Anyways, I hardly say "no" to new features proposals before testing them so I'll take a deeper look into this once I get some spare time.

    Thanks for your feedback!

  3. learnthat
    Member
    Posted 4 years ago #

    Hi Ikki,

    It actually wasn't too difficult to update the plug-in to support custom post types. Here's what I did - if you'd like, I can send you the file.

    General steps:

    1. Remove the current post/page options.
    2. Query the public custom post types and generate a select list.
    3. Add in support for the option.
    4. Change $nopages to the new SQL query variable based on the value of the post type select field.

    Actual code:

    Comment out this line:

    //$instance['pages'] = $new_instance['pages'];

    Add this line around line 172:

    $instance['posttype'] = $new_instance['posttype'];

    Comment out this line in the form function:

    //'pages' => true,

    Add in this line in the form function around line 219:

    'posttype' => 'all',

    Delete the checkbox section for 'pages' on the form.

    Add this code to the form for the widget above the thumbnail section:

    <fieldset class="widefat">
    			<legend><?php _e('Select Post Types', 'wordpress-popular-posts'); ?></legend>
    			<select id="<?php echo $this->get_field_id( 'posttype' ); ?>" name="<?php echo $this->get_field_name( 'posttype' ); ?>">
    			<option value="all"<?php if ( $instance['posttype'] == "all" ) : ?> selected="selected"<?php endif; ?>><?php _e('All', 'wordpress-popular-posts'); ?></option>
    			<option value="pagesposts"<?php if ( $instance['posttype'] == "pagesposts" ) : ?> selected="selected"<?php endif; ?>><?php _e('Posts and Pages', 'wordpress-popular-posts'); ?></option>
    			   	<?php
    				// Thank you to John James Jacoby for this code from post-type-switcher
    				$safe_filter = array( 'public' => true, 'show_ui' => true );
    	            $post_types = get_post_types( (array)$safe_filter );
    
    	foreach ( $post_types as $post_type ) {
    			$pt = get_post_type_object( $post_type );
    ?>
    			<option value="<?php echo $pt->name; ?>"<?php if ( $instance['posttype'] == $pt->name ) : ?> selected="selected"<?php endif; ?>><?php echo $pt->label; ?></option>
    <?php
    		} ?>
    			</select>
    			</fieldset>
    			<br />

    Near the top of the get_popular_posts function, add this code and comment out the old code:

    // Support custom post types
    			if ( $instance['posttype'] == 'all' ) {
                     $nopages = '';
                } else {
                  if ( $instance['posttype'] == 'pagesposts' ) {
                     $nopages = "AND ($wpdb->posts.post_type = 'post' OR $wpdb->posts.post_type = 'pages')";
                  } else {
                     $nopages = "AND $wpdb->posts.post_type = '" .$instance['posttype'] . "'";
                  }
                }			  
    
                // Deprecated with post-type support above
    			//if ( $instance['pages'] ) {
    			//	$nopages = '';
    			//} else {
    			//	$nopages = "AND $wpdb->posts.post_type = 'post'";
    			//}

    If you see anything I did wrong, please let me know, but it seems to work fine. Thanks for creating such a great, easy to use plug in.

    Jeremy

  4. learnthat
    Member
    Posted 4 years ago #

    I posted a link the full version of this file here: http://jerm.com/wordpress-popular-posts.php.txt

    Please let me know if you decide to use this - otherwise I will need to fork it so the end users do not accidentally upgrade some day and lose these changes. I appreciate all your hard work,

    Thanks!
    Jeremy

  5. learnthat
    Member
    Posted 4 years ago #

    One caveat that I didn't think of: the exclude categories portion of the widget likely won't work with custom post types in their current form. Custom post types have the ability to create custom taxonomies, but I'm sure querying based on those taxonomies use slightly different code. This may be something I need in the future and I'll address it at some point.

    I believe, you would have to provide an additional option and change this line:

    WHERE x.taxonomy = 'category'

    to dynamic based on the taxonomy selected.

  6. Héctor Cabrera
    Member
    Plugin Author

    Posted 4 years ago #

    Hi Jeremy,

    Sorry for the late response, just noticed you posted here now. Anyways, yes I am planning to include this on a future version of my plugin.

    The issue about custom taxonomies and the category exclusion feature was something I didn't think of either. Right now, I'm working on this and will do some tests hoping nothing breaks.

    There's too much going on around WordPress 3.0, it's driving me crazy! LOL.

  7. Derek Perkins
    Member
    Posted 4 years ago #

    I would love to have custom post types and taxonomies supported. It is absolutely vital for a few projects I'm working on and I love this plugin, so I'd rather not switch to another one.

  8. Derek Perkins
    Member
    Posted 4 years ago #

    Any idea on a release date that will include custom post types?

  9. Héctor Cabrera
    Member
    Plugin Author

    Posted 3 years ago #

    Nope, there's no ETA for this my friend. Still working on this and some other new features. This will take some time.

  10. sketchworks
    Member
    Posted 3 years ago #

    .

  11. Mark
    Member
    Posted 3 years ago #

    Thanks ikki, I'm looking forward to it.

  12. Derek Perkins
    Member
    Posted 3 years ago #

    I'm also looking forward to it. Thanks for the great plugin ikki!

  13. Jeff
    Member
    Posted 3 years ago #

    One thing I noticed and am hoping to find a solution now, is the above code examples work great for the widget, however direct access through shortcode or wpp_get_mostpopular. Even if you pass posttype along as a param it does not get picked by wpp_shortcode. I added the line
    'posttype' => empty($posttype) ? "all" : $posttype, to the $shortcode_ops, however $posttype is never defined. I assumed

    extract( shortcode_atts( array(
                    'header' => '',
                    'limit' => 10,
                    'range' => 'daily',
                    'order_by' => 'comments',
                    'pages' => true,
                    'title_length' => 0,
                    'excerpt_length' => 0,
                    'excerpt_format' => 0,
                    'cats_to_exclude' => '',
                    'thumbnail_width' => 0,
                    'thumbnail_height' => 0,
                    'thumbnail_selection' => 'wppgenerated',
                    'rating' => false,
                    'stats_comments' => true,
                    'stats_views' => false,
                    'stats_author' => false,
                    'stats_date' => false,
                    'stats_date_format' => 'F j, Y',
                    'wpp_start' => '<ul>',
                    'wpp_end' => '</ul>',
                    'post_start' => '<li>',
                    'post_end' => '</li>',
                    'header_start' => '<h2>',
                    'header_end' => '</h2>',
                    'do_pattern' => false,
                    'pattern_form' => '{image} {title}: {summary} {stats}'
                ), $atts ) );

    would pre-define it however it does not.

  14. simosmme
    Member
    Posted 3 years ago #

    it would be also interesting if we could display most popular articles that were posts daily or weekly

    Right now in the daily posts i can also see posts edited years ago but with good SEO.

    I would like a widget, based on WordPress Popular Posts to diplay most views today but that were edited the last 24 or 48 hours.

    How about that?

  15. cpkid2
    Member
    Posted 3 years ago #

    Would LOVE this feature for custom post types!!

    Jeremy, I copied your altered code, named it wordpress-popular-posts.php and replaced it with the original wordpress-popular-posts.php in the plugin folder and I got "Sorry, no data found". Is there something else I need to do?

  16. cpkid2
    Member
    Posted 3 years ago #

    By the way, I'm using the template tag and currently calling the function like this:

    <?php if (function_exists('wpp_get_mostpopular')) wpp_get_mostpopular("limit=4&range=all&order_by=views&pages=0&thumbnail_width=23&thumbnail_height=23&thumbnail_selection=usergenerated&stats_comments=0&stats_views=1&stats_date=1&stats_date_format='M j, Y'&wpp_start=\"<ul id='popular-posts-content'>\""); ?>

  17. learnthat
    Member
    Posted 3 years ago #

    I'm not sure, I used this on a client project and haven't looked at this in a long time. Have you tried using it in a widget? I think the changes I made would only allow you to filter by custom post type when used in a widget.

  18. cpkid2
    Member
    Posted 3 years ago #

    I'm incorporating the Popular Posts into a tab powered by jQuery so using a widget wouldn't be possible (I think). Thank you for taking the time to reply, anyway.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic