我建议您首先按搜索词搜索附件。例如:
$attachments_query = new WP_Query( array(
\'s\' => \'Your search query\', // <-- Your search term
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'application/pdf\',
\'post_status\' => \'inherit\',
\'posts_per_page\' => -1,
) );
通过此查询,您将获得符合搜索查询条件的所有PDF文件。此查询只需要附件ID,若要获取它们,请使用以下函数:
$attachments_ids = wp_list_pluck( $attachments_query->posts, \'ID\' );
现在,当您有附件ID时,可以在ACF文件字段中查找具有任何这些ID的帖子(ACF将ID存储在数据库中的文件字段中)。要获取帖子,请运行以下查询:
$query = new WP_Query( array(
\'post_type\' => \'your_post_type\', // <-- Your post type here
\'posts_per_page\' => -1,
\'meta_query\' => array(
array(
\'key\' => \'your_pdf_field_key\', // <-- ACF field name
\'value\' => $attachments_ids,
\'compare\' => \'IN\'
)
)
) );
代码未经测试,但它必须选择所有带有搜索附件的帖子。
以下是文档的链接:
附件查询:https://developer.wordpress.org/reference/classes/wp_query/#mime-type-parameters
wp\\u list\\u pulk函数:https://developer.wordpress.org/reference/functions/wp_list_pluck/https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters