Advertisement
Guest User

WordPress template to list a category by custom field

a guest
Apr 1st, 2010
1,883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package WordPress
  4.  * @subpackage Default_Theme
  5.  */
  6. /*
  7. Template Name: listorderbycustom
  8. */
  9. /*
  10.  Author: Mac McDonald
  11.  Contact at BluegrassMiataClub.com using About->Contact Us form.
  12.  
  13.  This program selects posts in a specified category,
  14.  ordered by date within the presence of a specified Custom Field,
  15.  i.e. those having the CF will be listed first.
  16.  
  17. Set the following variables to your needs:
  18. */
  19. $meta_key = 'URL';
  20. $catname = 'Hotel';
  21.  
  22. add_filter('posts_fields','mam_posts_fields');
  23. add_filter('posts_join','mam_posts_join');
  24. add_filter('posts_orderby','mam_posts_orderby');
  25. $paged = (isset($_GET['paged'])) ? intval($_GET['paged']) : 1;
  26. ?>
  27.  
  28. <?php get_header(); ?>
  29.     <div id="content" class="narrowcolumn" role="main">
  30.  
  31. <?php
  32. $mam_global_fields = 'wpmeta.meta_value, if (isnull(wpmeta.meta_value),1,0) as sorturl';
  33. $mam_global_join = "LEFT JOIN $wpdb->postmeta wpmeta ON
  34.         ({$wpdb->posts}.ID = wpmeta.post_id AND wpmeta.meta_key = '$meta_key')";
  35. $mam_global_orderby = 'sorturl';
  36. query_posts("category_name=$catname&caller_get_posts=1&paged=$paged");
  37. echo "<h2>Posts in Category $catname</h2><br />";
  38. ?>
  39.     <?php if (have_posts()) : echo '<ul>' ?>
  40.  
  41.         <?php while (have_posts()) : the_post(); ?>
  42.                 <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li><br />
  43.         <?php endwhile; ?>
  44.  
  45.         <div class="navigation">
  46.             <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
  47.             <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
  48.         </div></ul>
  49.  
  50.     <?php else : ?>
  51.  
  52.         <h2 class="center">Not Found</h2>
  53.         <p class="center">Sorry, but you are looking for something that isn't here.</p>
  54.         <?php get_search_form(); ?>
  55.  
  56.     <?php endif; ?>
  57.  
  58. </div><!-- End content -->
  59. <?php get_footer(); ?>
  60. <?php function mam_posts_fields ($fields) {
  61. global $mam_global_fields;
  62. return "$fields, $mam_global_fields";
  63. }
  64. function mam_posts_join ($join) {
  65. global $mam_global_join;
  66. return "$join $mam_global_join";
  67. }
  68. function mam_posts_orderby ($orderby) {
  69. global $mam_global_orderby;
  70. return "$mam_global_orderby, $orderby";
  71. }?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement