Advertisement
Guest User

Sorting with PHP WORDPRESS

a guest
Sep 17th, 2012
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. RESOURCE LIBRARY EXAMPLE:
  2.  
  3. HTML:
  4. <div class="sort">
  5.        <a href="/resources/?sort=alpha">alpha</a> |
  6.        <a href="/resources/?sort=recent">recent</a> |
  7.        <a href="/resources/?sort=oldest">oldest</a>
  8. </div>
  9.  
  10. PHP:
  11. <?php
  12. wp_reset_query();
  13. $args = array(
  14.     'post_type' => 'resource',
  15.     'posts_per_page' => -1,
  16. );
  17.  
  18. if (isset($_GET['sort']))
  19. {
  20.     if ('alpha' == $_GET['sort'])
  21.     {
  22.         $args['orderby'] = 'title';
  23.         $args['order'] = 'ASC';
  24.     }
  25.    
  26.     elseif ('oldest' == $_GET['sort'])
  27.     {
  28.         $args['order'] = 'ASC';
  29.         $args['orderby'] = 'date';
  30.     }
  31.  
  32.     elseif ('recent' == $_GET['sort'])
  33.     {
  34.         $args['order'] = 'DESC';  
  35.         $args['orderby'] = 'date';
  36.     }
  37. }
  38.  
  39. query_posts($args);
  40. ?>
  41.  
  42. In your case you could make the sort links (the HTML provided) into numbers for posts per page.
  43.  
  44. SORT BY NUMBER OF POSTS:
  45.  
  46. EXAMPLE:
  47.  
  48. HTML:
  49. <div class="sort">
  50.        <a href="/resources/?sort=25">25</a> |
  51.        <a href="/resources/?sort=50">50</a> |
  52.        <a href="/resources/?sort=100">100</a> |
  53.        <a href="/resources/?sort=all">All</a>
  54. </div>
  55.  
  56.  Then for your args you could do something like the following (I only did 2 of them):
  57.  
  58. PHP:
  59. <?php
  60. wp_reset_query();
  61. $args = array(
  62.     'posts_per_page' => -1,
  63.     'orderby' => DESC,
  64. );
  65.  
  66. if (isset($_GET['sort']))
  67. {
  68.  if ('25' == $_GET['sort'])
  69.     {
  70.         $args['posts_per_page'] = '25';
  71.     }
  72. elseif ('all' == $_GET['sort'])
  73.     {
  74.        $args['posts_per_page'] = '99999999';
  75.      }
  76. }
  77.  
  78. query_posts($args);
  79. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement