wp_ajax_query_attachments() - WordPress 函数参考中文文档

返回介绍

wp_ajax_query_attachments()

发布于 2017-09-11 字数2807 浏览 917 评论 0

wp_ajax_query_attachments()

Ajax handler for querying attachments.


描述


源代码

File: wp-admin/includes/ajax-actions.php

function wp_ajax_query_attachments() {
	if ( ! current_user_can( 'upload_files' ) )
		wp_send_json_error();

	$query = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array();
	$keys = array(
		's', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type',
		'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum'
	);
	foreach ( get_taxonomies_for_attachments( 'objects' ) as $t ) {
		if ( $t->query_var && isset( $query[ $t->query_var ] ) ) {
			$keys[] = $t->query_var;
		}
	}

	$query = array_intersect_key( $query, array_flip( $keys ) );
	$query['post_type'] = 'attachment';
	if ( MEDIA_TRASH
		&& ! empty( $_REQUEST['query']['post_status'] )
		&& 'trash' === $_REQUEST['query']['post_status'] ) {
		$query['post_status'] = 'trash';
	} else {
		$query['post_status'] = 'inherit';
	}

	if ( current_user_can( get_post_type_object( 'attachment' )->cap->read_private_posts ) )
		$query['post_status'] .= ',private';

	// Filter query clauses to include filenames.
	if ( isset( $query['s'] ) ) {
		add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
	}

	/**
	 * Filters the arguments passed to WP_Query during an Ajax
	 * call for querying attachments.
	 *
	 * @since 3.7.0
	 *
	 * @see WP_Query::parse_query()
	 *
	 * @param array $query An array of query variables.
	 */
	$query = apply_filters( 'ajax_query_attachments_args', $query );
	$query = new WP_Query( $query );

	$posts = array_map( 'wp_prepare_attachment_for_js', $query->posts );
	$posts = array_filter( $posts );

	wp_send_json_success( $posts );
}

更新日志

Version 描述
3.5.0 Introduced.

相关函数

Uses

  • wp-admin/includes/ajax-actions.php:
    ajax_query_attachments_args
  • wp-includes/capabilities.php:
    current_user_can()
  • wp-includes/class-wp-query.php:
    WP_Query::__construct()
  • wp-includes/functions.php:
    wp_send_json_error()
  • wp-includes/functions.php:
    wp_send_json_success()
  • wp-includes/plugin.php:
    add_filter()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/media.php:
    get_taxonomies_for_attachments()
  • wp-includes/post.php:
    get_post_type_object()
  • Show 4 more uses
    Hide more uses


User Contributed Notes

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!