admin管理员组

文章数量:1321070

I am using advanced custom fields to create a custom field. I created a custom field called upload pdf. I can attach the pdf file using this field and the field type is File. Suppose there is a publication page in WordPress. Publications are showing as a PDF file on the page. And those PDF are coming from a custom field. There is a search option on the page to search for PDF files. When you type the PDF file name in the search bar, it should show the results. I don't want to use any plugins for that.

Without using any plugins, is it possible to search those pdf files?

I am using advanced custom fields to create a custom field. I created a custom field called upload pdf. I can attach the pdf file using this field and the field type is File. Suppose there is a publication page in WordPress. Publications are showing as a PDF file on the page. And those PDF are coming from a custom field. There is a search option on the page to search for PDF files. When you type the PDF file name in the search bar, it should show the results. I don't want to use any plugins for that.

Without using any plugins, is it possible to search those pdf files?

Share Improve this question edited Sep 30, 2020 at 5:57 Falak asked Sep 28, 2020 at 9:22 Falak Falak 1012 bronze badges 2
  • You mean search the text inside the files? I don't think so, no - WordPress doesn't have PDF text extraction built in I don't think, nor does it store a searchable copy of the text the in database. You may be able to add that yourself though if you can find a PDF to text library or tool you can invoke to extract the text for you, but it seems simpler to use a plugin someone's already written to do this. – Rup Commented Sep 29, 2020 at 16:20
  • @Rup No. I don't want to search text inside the pdf. Suppose there is a publication page in WordPress. Publications are showing as a PDF file on the page. And those PDF are coming from a custom field. There is a search option on the page to search for PDF files. When you type the PDF file name in the search bar, it should show the results. – Falak Commented Sep 30, 2020 at 5:56
Add a comment  | 

1 Answer 1

Reset to default 0

I would recommend you first to search attachments by your search term. For example:

$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,
) );

With this query, you will get all PDF files that fit your search query term. All you need from this query, is attachments IDs, to get them, use this function:

$attachments_ids = wp_list_pluck( $attachments_query->posts, 'ID' );

Now, when you have attachment IDs, you can look for posts that have any of those IDs inside ACF File field (ACF stores ID inside File filed in database). To get posts, run this query:

$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'
        )
    )
) );

The code is not tested, but it must select you all posts with searched attachments.

Here are also links to the documentation:

  1. Attachments query: https://developer.wordpress/reference/classes/wp_query/#mime-type-parameters
  2. wp_list_pluck function: https://developer.wordpress/reference/functions/wp_list_pluck/
  3. WP_Meta_Query: https://developer.wordpress/reference/classes/wp_query/#custom-field-post-meta-parameters

本文标签: advanced custom fieldsHow to search pdf attachment