| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: Query Standard Format Posts |
|---|
| 4 | Description: Allows you to query posts with the standard post format |
|---|
| 5 | Version: 0.1 |
|---|
| 6 | Author: Mark Jaquith & Andrew Nacin |
|---|
| 7 | Author URI: http://wordpress.org/ |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | class CWS_Query_Standard_Format_Posts { |
|---|
| 11 | static $instance; |
|---|
| 12 | |
|---|
| 13 | function __construct() { |
|---|
| 14 | self::$instance = $this; // So other plugins can remove our hooks |
|---|
| 15 | add_action( 'init', array( &$this, 'init' ) ); |
|---|
| 16 | register_activation_hook( __FILE__, array( $this, 'activate' ) ); |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | function init() { |
|---|
| 20 | add_filter( 'request', array( $this, 'request' ), 5 ); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | function activate() { |
|---|
| 24 | if ( ! term_exists( 'post-format-standard', 'post_format' ) ) |
|---|
| 25 | wp_insert_term( 'post-format-standard', 'post_format' ); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | function request( $qvs ) { |
|---|
| 29 | if ( ! isset( $qvs['post_format'] ) ) |
|---|
| 30 | return $qvs; |
|---|
| 31 | $slugs = array_flip( get_post_format_slugs() ); |
|---|
| 32 | $by_raw_slug = get_post_format_slugs(); |
|---|
| 33 | $by_translated_slug = array_flip( $by_raw_slug ); |
|---|
| 34 | if ( 'standard' == $by_translated_slug[ $qvs['post_format'] ] ) { |
|---|
| 35 | if ( isset( $slugs[ $qvs['post_format'] ] ) ) |
|---|
| 36 | $qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ]; |
|---|
| 37 | // If 'standard', then query every persistent format with a NOT IN instead. |
|---|
| 38 | unset( $qvs['post_format'] ); |
|---|
| 39 | $formats = array(); |
|---|
| 40 | $raw_slugs = array_diff( array_keys( $by_raw_slug ), array( 'standard' ) ); |
|---|
| 41 | foreach ( $raw_slugs as $format ) { |
|---|
| 42 | $formats[] = 'post-format-' . $format; |
|---|
| 43 | } |
|---|
| 44 | if ( ! isset( $qvs['tax_query'] ) ) |
|---|
| 45 | $qvs['tax_query'] = array(); |
|---|
| 46 | $qvs['tax_query'][] = array( 'taxonomy' => 'post_format', 'terms' => $formats, 'field' => 'slug', 'operator' => 'NOT IN' ); |
|---|
| 47 | $qvs['tax_query']['relation'] = 'AND'; |
|---|
| 48 | // Repair the query flags and queried object. |
|---|
| 49 | add_action( 'parse_query', array( $this, 'parse_query' ) ); |
|---|
| 50 | } |
|---|
| 51 | // Only post types that support formats should be queried. |
|---|
| 52 | $tax = get_taxonomy( 'post_format' ); |
|---|
| 53 | $qvs['post_type'] = $tax->object_type; |
|---|
| 54 | return $qvs; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | function parse_query( $q ) { |
|---|
| 58 | $q->is_tax = $q->is_archive = true; |
|---|
| 59 | $q->is_home = false; |
|---|
| 60 | $q->queried_object = get_term_by( 'slug', 'post-format-standard', 'post_format' ); |
|---|
| 61 | $q->queried_object_id = $q->queried_object->term_id; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | new CWS_Query_Standard_Format_Posts; |
|---|