php 您可能希望扩展WP_Query类的方式和原因的实际示例。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 您可能希望扩展WP_Query类的方式和原因的实际示例。相关的知识,希望对你有一定的参考价值。

<?php

/**
 * Class Video_Query
 */
class Video_Query extends Attachment_Query {

	function __construct( $query = '' ) {
		add_filter( 'posts_where', array( $this, 'posts_where' ) );
		parent::__construct( wp_parse_args( $query ) );
		remove_filter( 'posts_where', array( $this, 'posts_where' ) );
	}

	function posts_where( $where ) {
		/**
		 * @var wpdb $wpdb
		 */
		global $wpdb;
		$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_mime_type LIKE %s", like_escape( 'video/' ) . '%' );
		return $where;
	}

}
<?php

/**
 * Class Image_Query
 */
class Image_Query extends Attachment_Query {

	function __construct( $query = '' ) {
		add_filter( 'posts_where', array( $this, 'posts_where' ) );
		parent::__construct( wp_parse_args( $query ) );
		remove_filter( 'posts_where', array( $this, 'posts_where' ) );
	}

	function posts_where( $where ) {
		/**
		 * @var wpdb $wpdb
		 */
		global $wpdb;
		$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_mime_type LIKE %s", like_escape( 'image/' ) . '%' );
		return $where;
	}

}
<?php

/**
 * Class Document_Query
 */
class Document_Query extends Attachment_Query {

	function __construct( $query = '' ) {
		add_filter( 'posts_where', array( $this, 'posts_where' ) );
		parent::__construct( wp_parse_args( $query ) );
		remove_filter( 'posts_where', array( $this, 'posts_where' ) );
	}

	function posts_where( $where ) {
		/**
		 * @var wpdb $wpdb
		 */
		global $wpdb;
		$where .= $wpdb->prepare( " AND ( {$wpdb->posts}.post_mime_type LIKE %s", like_escape( 'application/' ) . '%' );
		$where .= $wpdb->prepare( " OR {$wpdb->posts}.post_mime_type LIKE %s )", like_escape( 'text/' ) . '%' );
		return $where;
	}

}
<?php

/**
 * Class Audio_Query
 */
class Audio_Query extends Attachment_Query {

	function __construct( $query = '' ) {
		add_filter( 'posts_where', array( $this, 'posts_where' ) );
		parent::__construct( wp_parse_args( $query ) );
		remove_filter( 'posts_where', array( $this, 'posts_where' ) );
	}

	function posts_where( $where ) {
		/**
		 * @var wpdb $wpdb
		 */
		global $wpdb;
		$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_mime_type LIKE %s", like_escape( 'audio/' ) . '%' );
		return $where;
	}

}
<?php

/**
 * Class Attachment_Query
 */
class Attachment_Query extends WP_Query {

	function __construct( $query = '' ) {
		$defaults = array(
			'post_type'      => 'attachment',
			'post_status'    => 'inherit',
			'posts_per_page' => - 1,
			'orderby'        => 'menu_order',
			'order'          => 'ASC',
		);
		parent::__construct( wp_parse_args( $query, $defaults ) );
	}

}

以上是关于php 您可能希望扩展WP_Query类的方式和原因的实际示例。的主要内容,如果未能解决你的问题,请参考以下文章

php Wordpress Bootstrap 4.1分页(使用自定义WP_Query()和全局$ wp_query支持)

php WP_query

php 优化WP_QUERY

php wp_query

php WP_QUERY示例

php WP_Query