WordPress.org

Plugin Directory

Changeset 187001


Ignore:
Timestamp:
12/25/09 13:41:32 (8 years ago)
Author:
trick77
Message:

new sorting options

Location:
must-read-posts/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • must-read-posts/trunk/must-read-posts.php

    r115226 r187001  
    44Plugin URI: http://www.trick77.com/2008/12/29/wordpress-plugin-widget-must-read-posts 
    55Description: Retrieves posts and pages with a certain custom field (e.g. to permanently show your most recommended posts in a widget) and displays them in a list. Use it as a widget or place it in your templates or exec-php fragments using <code>&lt;?php get_mustread(); ?&gt;</code> 
    6 Version: 1.0.7 
     6Version: 2.0.0 
    77Author: trick77 
    88Author URI: http://www.trick77.com/ 
     
    1515    $options = get_option("trick77_widget_mustread"); 
    1616    if (!is_array( $options )) { 
    17         $options = array('title' => 'Must Read Posts', 'limit' => 10, 'fkey' => 'must-read'); 
     17        $options = array('title' => 'Must Read Posts', 'limit' => 10, 'fkey' => 'must-read', 'sorder' => 'custom'); 
    1818    } 
    1919     
     
    2323    echo $after_title; 
    2424     
    25     global $wpdb, $post; 
     25    global $wpdb, $post, $sortcrit; 
    2626     
    27     $sql = "SELECT $wpdb->posts.ID, post_title, post_name, post_date FROM $wpdb->posts, $wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND meta_key = '". $options['fkey'] ."' AND meta_value like 'true%' AND post_date_gmt < '".gmdate("Y-m-d H:i:s")."' AND post_status = 'publish' AND post_password = '' ORDER BY meta_value LIMIT ". $options['limit'] .""; 
     27    switch ($options['sorder']) { 
     28        case "date_asc":  
     29            $sortcrit = "post_date ASC";  
     30            break; 
     31        case "date_desc":  
     32            $sortcrit = "post_date DESC";  
     33            break; 
     34        case "rnd":  
     35            $sortcrit = "RAND()";  
     36            break; 
     37        case "custom":  
     38            $sortcrit = "meta_value";  
     39            break; 
     40        default:  
     41            $sortcrit = "meta_value";  
     42            break; 
     43    } 
     44 
     45    $sql = "SELECT $wpdb->posts.ID, post_title, post_name, post_date FROM $wpdb->posts, $wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND meta_key = '". $options['fkey'] ."' AND meta_value like 'true%' AND post_date_gmt < '".gmdate("Y-m-d H:i:s")."' AND post_status = 'publish' AND post_password = '' ORDER BY ". $sortcrit ." LIMIT ". $options['limit'] .""; 
    2846    $mustread = $wpdb->get_results($sql); 
    2947    echo "<ul>"; 
     
    4058 
    4159  if (!is_array( $options )) { 
    42         $options = array('title' => 'Must Read Posts', 'limit' => 10, 'fkey' => 'must-read'); 
     60        $options = array('title' => 'Must Read Posts', 'limit' => 10, 'fkey' => 'must-read', 'sorder' => 'custom'); 
    4361  }       
    44  
     62   
    4563  if ($_POST['mustread-Submit']) { 
    4664        $options['title'] = htmlspecialchars($_POST['mustread-WidgetTitle']); 
    4765    $options['limit'] = htmlspecialchars($_POST['mustread-Limit']); 
    4866    $options['fkey'] = htmlspecialchars($_POST['mustread-Fkey']); 
     67    $options['sorder'] = htmlspecialchars($_POST['mustread-SOrder']); 
     68     
    4969     
    5070    if ($options['title'] == '') $options['title'] = "Must Read Posts"; 
     
    5373     
    5474    if ($options['fkey'] == '') $options['fkey'] = "must-read"; 
     75 
     76    if ($options['sorder'] == '') $options['sorder'] = "custom"; 
    5577     
    56  
    5778    update_option("trick77_widget_mustread", $options); 
    5879  } 
     
    6485    <label for="mustread-Limit">Show only </label> 
    6586    <input type="text" id="mustread-Limit" name="mustread-Limit" value="<?php echo $options['limit'];?>" class="widefat" style="width:27px" /> posts<br />     
    66     <label for="mustread-Fkey">Custom field name key </label> 
     87    <label for="mustread-Fkey">Custom field name key:</label> 
    6788    <input type="text" id="mustread-Fkey" name="mustread-Fkey" value="<?php echo $options['fkey'];?>" class="widefat" /><br />     
    68     <input type="hidden" id="mustread-Submit" name="mustread-Submit" value="1" />  
     89    <br /><label for="mustread-SOrder">Sort order:</label><br /> 
     90        <input type="radio" id="mustread-SOrder" name="mustread-SOrder" value="date_asc" <?php if ($options['sorder'] == 'date_asc') echo " checked "; ?> />&nbsp;Date (oldest first)<br />  
     91        <input type="radio" id="mustread-SOrder" name="mustread-SOrder" value="date_desc" <?php if ($options['sorder'] == 'date_desc') echo " checked "; ?> />&nbsp;Date (latest first)<br />  
     92        <input type="radio" id="mustread-SOrder" name="mustread-SOrder" value="rnd" <?php if ($options['sorder'] == 'rnd') echo " checked "; ?> />&nbsp;Random<br />  
     93        <input type="radio" id="mustread-SOrder" name="mustread-SOrder" value="custom" <?php if ($options['sorder'] == 'custom') echo " checked "; ?> />&nbsp;Custom (Tag ordering)<br />      
     94    <input type="hidden" id="mustread-Submit" name="mustread-Submit" value="1" />  
    6995  </p> 
    7096<?php 
     
    84110 
    85111// plugin core 
    86 function get_mustread($title = "Must Read Posts", $limit = 15, $fkey = 'must-read' ) { 
    87     global $wpdb, $post; 
     112function get_mustread($title = "Must Read Posts", $limit = 15, $fkey = 'must-read', $sorder = 'custom' ) { 
     113    global $wpdb, $post, $sortcrit; 
    88114     
    89115    if ((!is_string($title)) || ($title == '')) $title = "Must Read Posts"; 
    90116    if ((!is_numeric($limit)) || ($limit <= 0) || ($limit == '')) $limit = 25; 
    91117    if ((!is_string($fkey)) || ($fkey == '')) $fkey = "must-read"; 
     118    if ((!is_string($sorder)) || ($sorder == '')) $sorder = "custom"; 
    92119     
    93     $mustread = $wpdb->get_results("SELECT $wpdb->posts.ID, post_title, post_name, post_date FROM $wpdb->posts, $wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND meta_key = '$fkey' AND meta_value like 'true%' AND post_date_gmt < '".gmdate("Y-m-d H:i:s")."' AND post_status = 'publish' AND post_password = '' ORDER BY meta_value LIMIT $limit"); 
     120    switch ($sorder) { 
     121        case "date_asc":  
     122            $sortcrit = "post_date ASC";  
     123            break; 
     124        case "date_desc":  
     125            $sortcrit = "post_date DESC";  
     126            break; 
     127        case "rnd":  
     128            $sortcrit = "RAND()";  
     129            break; 
     130        case "custom":  
     131            $sortcrit = "meta_value";  
     132            break; 
     133        default:  
     134            $sortcrit = "meta_value";  
     135            break; 
     136    } 
     137 
     138    $sql = "SELECT $wpdb->posts.ID, post_title, post_name, post_date FROM $wpdb->posts, $wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND meta_key = '". $fkey ."' AND meta_value like 'true%' AND post_date_gmt < '".gmdate("Y-m-d H:i:s")."' AND post_status = 'publish' AND post_password = '' ORDER BY " . $sortcrit ." ". $options['limit'] .""; 
     139    $mustread = $wpdb->get_results($sql); 
    94140     
    95141     
  • must-read-posts/trunk/readme.txt

    r115226 r187001  
    33Tags: posts, widgets, plugin, sidebar, links, page 
    44Requires at least: 2.5 
    5 Tested up to: 2.7.1 
    6 Stable tag: 1.0.7 
     5Tested up to: 2.9 
     6Stable tag: 2.0.0 
    77 
    88Retrieves posts and pages with a certain custom field (e.g. to permanently show your most recommended posts in a widget) and displays them in a list. 
     
    1414= What's new = 
    1515 
    16 Added: Sort-order can be customized now (see below). 
     16Added: Several sorting options added, check out the widget configuration area. 
    1717 
    1818= Use it as a Widget = 
     
    2222= Use it in a .php page = 
    2323 
    24 Just put `<?php get_mustread(); ?>` in your template. You could even use it within your posts or pages using the [Exec-PHP plugin](http://wordpress.org/extend/plugins/exec-php/) that allows you to put php code fragments directly into the text. This allows you to create a static page or a post showing your most recommended posts. The get_mustread function takes 3 **optional** arguments: title, maximum number of posts to be shown, name of the custom tag. Optional means you don't have to specify any parameters if you don't need to. 
     24Just put `<?php get_mustread(); ?>` in your template. You could even use it within your posts or pages using the [Exec-PHP plugin](http://wordpress.org/extend/plugins/exec-php/) that allows you to put php code fragments directly into the text. This allows you to create a static page or a post showing your most recommended posts. The get_mustread function takes 4 **optional** arguments: title, maximum number of posts to be shown, name of the custom tag, sort order. Optional means you don't have to specify any parameters if you don't need to. 
    2525 
    2626= Marking a post or page to be included = 
     
    2929If you don't want the custom field to be named *must-read* you can also change it to something else. The custom field value always has to start with *true* for the page or post to appear in the list. 
    3030 
    31 = Sort order = 
     31= Custom sort order = 
    3232 
    33 You can also change the sort-order of the links to your posts or pages in the Must-Read-Posts list. Just add a semi-colon and a sort-number after *true* in the custom value field. The field value for your 1st post would be *true;01* and the 2nd field would like *true;02* and so on. This gives you full flexibility over the positioning of your posts or pages in the Must-Read-Posts list. If you mix sorted and unsorted posts or pages, the ones with no number will be sorted first. If you have a bunch of posts or pages that you just want to sort at the end use something like *true;99* on all of them to move them at the end of the sort order.  
     33The plugin features several self explanatory sorting options. You can also specify a custom sort order. Just add a semi-colon and a sort-number after *true* in the custom value field. The field value for your 1st post would be *true;01* and the 2nd field would like *true;02* and so on. This gives you full flexibility over the positioning of your posts or pages in the Must-Read-Posts list. If you mix sorted and unsorted posts or pages, the ones with no number will be sorted first. If you have a bunch of posts or pages that you just want to sort at the end use something like *true;99* on all of them to move them at the end of the sort order.  
    3434 
    3535= Remove a post or page from the Must-ReadPosts list = 
Note: See TracChangeset for help on using the changeset viewer.