posts_where

最后更新于:2021-11-27 15:53:22

apply_filters_ref_array( ‘posts_where’, string $where, GC_Query $query )

Filters the WHERE clause of the query.

参数

$where

(string)
The WHERE clause of the query.

$query

(GC_Query)
The GC_Query instance (passed by reference).

add_filter( 'posts_where' , 'posts_where' );

function posts_where( $where ) {

	if( is_admin() ) {
		global $gcdb;
		
		if ( isset( $_GET['author_restrict_posts'] ) && !empty( $_GET['author_restrict_posts'] ) && intval( $_GET['author_restrict_posts'] ) != 0 ) {
			$author = intval( $_GET['author_restrict_posts'] );
		
			$where .= " AND ID IN (SELECT object_id FROM {$gcdb->term_relationships} WHERE term_taxonomy_id=$author )";
		}
	}
	return $where;
}

源文件

文件: gc-includes/class-gc-query.php

View on Trac

    add_filter( 'posts_where' , 'posts_where', 10, 2);
    function posts_where( $args, $gc_query_obj ) {
      $type = $gc_query_obj->query_vars['post_type'];
      switch(true){
        case 'any'==$type: //query any type (see codex for more details).
          break;
        case !empty($type) && is_array($type):
          //multiple post types define
          break;
        case !empty($type):
          //post type is a custom post.
          break;
        case $gc_query_obj->is_attachment():
          //post type is empty but is a media.
          $type='attachment';
          break;
        case $gc_query_obj->is_page():
          //post type is empty but is a page.
          $type='page';
          break;
        default:
          //post type is empty and not an attachment nor a page, so is a post.
          $type='post';
          break;
      }
      return $where;
    }