Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sort posts from last 24 hrs in order on presence of a custom field, then date
- $meta_key = 'featured'; // The meta_key
- function mam_posts_fields ($fields) {
- global $mam_global_fields;
- // Make sure there is a leading comma
- if ($mam_global_fields) $fields .= (preg_match('/^(\s+)?,/',$mam_global_fields)) ? $mam_global_fields : ", $mam_global_fields";
- return $fields;
- }
- function mam_posts_join ($join) {
- global $mam_global_join;
- if ($mam_global_join) $join .= " $mam_global_join";
- return $join;
- }
- function mam_posts_where ($where) {
- global $mam_global_where;
- if ($mam_global_where) $where .= " $mam_global_where";
- return $where;
- }
- function mam_posts_orderby ($orderby) {
- global $mam_global_orderby;
- if ($mam_global_orderby) $orderby = $mam_global_orderby;
- return $orderby;
- }
- add_filter('posts_fields','mam_posts_fields');
- add_filter('posts_join','mam_posts_join');
- add_filter('posts_where','mam_posts_where');
- add_filter('posts_orderby','mam_posts_orderby');
- global $wpdb,$wp_query;
- $now = date('Y-m-d',(time() - 60*60*24));
- $mam_global_fields = ', IF(ISNULL(pm.meta_id),9,1) as sort_key';
- $mam_global_join = " LEFT JOIN $wpdb->postmeta pm ON ($wpdb->posts.ID = pm.post_id AND pm.meta_key = '$meta_key')";
- $mam_global_where = " AND $wpdb->posts.post_date > '$now' ";
- $mam_global_orderby = "sort_key, $wpdb->posts.post_date DESC";
- query_posts('posts_per_page=-1&ignore_sticky_posts=1');
- //echo 'REQUEST: ';print_r($wp_query->request);
- if (have_posts()) {
- while (have_posts()) {
- the_post();
- echo "<p>"; the_title(); echo ' '; the_time('Y-m-d'); echo " $post->sort_key</p>";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement