Posted by michael on Sat 7 Mar 13:33
report abuse | download | new post
- <?php
- /* Preconditions:
- 1. You need to display articles from just one category. (You need the category's ID!)
- 2. You have a custom field which includes a date written in the yyyy-mm-dd-format.
- 3. You need to sort your articles by the value of this very custom field in ascending order.
- 4. You do not want to show articles that are in the past - depending on the value of the custom field.
- 5. You want to show only published articles.
- Things you have to fill in are marked with [[ and ]]
- */
- $querystr = "
- SELECT * FROM $wpdb->posts
- LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
- LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
- LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
- WHERE $wpdb->term_taxonomy.term_id = [[ Category-ID ]]
- AND $wpdb->term_taxonomy.taxonomy = 'category'
- AND $wpdb->posts.post_status = 'publish'
- AND $wpdb->postmeta.meta_key = '[[ Name of Custom Field ]]'
- AND STR_TO_DATE($wpdb->postmeta.meta_value, '%Y-%m-%d') >= NOW()
- ORDER BY $wpdb->postmeta.meta_value ASC
- ";
- $posts = $wpdb->get_results($querystr);
- foreach ($posts as $post) :
- setup_postdata($post);
- ?>
- <!-- Whatever shall be displayed -->
- <?php endforeach; ?>
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.