Forums

How to make the search function include post_type = "attachment"? (9 posts)

  1. dcjob
    Member
    Posted 4 months ago #

    I uploaded some attachments with title and description. How to make the search function on front page to search these attachments?

  2. esmi
    Moderator
    Posted 4 months ago #

  3. dcjob
    Member
    Posted 4 months ago #

    I tried "search-everything", it does not work for search the description of the files in Media. I think it need to hack the codes of WP, make the searching include not only post_type="page" and "post", but also "attachment". Who knows how to do it? Thanks.

  4. t31os_
    Moderator
    Posted 4 months ago #

    In order to query for attachments you'd need to set post_status to inherit or any... (using "any", drafts etc.. will show up)..

  5. dcjob
    Member
    Posted 4 months ago #

    How to change the post_status by default? I want the post_status will be "any" when a user adds a file into Media. Thank you!

  6. t31os_
    Moderator
    Posted 3 months ago #

    I think you're missing the point, if you query for any, you'll also end up with Drafts and Revisions, etc... in the results..

  7. dcjob
    Member
    Posted 3 months ago #

    How to do it? In "Search" function, no options. Or I need add an entry in config file? Thank you!

  8. web00132
    Member
    Posted 3 months ago #

    I would also like to be able to include the attachment description when searching, this doesn't seem doable with the 'Search Everything' plugin. Don't get that stuff about setting something to 'any', what do you mean?

  9. t31os_
    Moderator
    Posted 3 months ago #

    The search query, by default is set to post, if you add a query arg into the query posts line of your search.php, you can do any number of things..

    if(!$wp_query) global $wp_query;
    $args = array(
    'post_type' => 'any',
    'category' => 3,
    'author' => 1
    );
    query_posts( array_merge( $args , $wp_query->query ) );

    There's a few example args in the above... ;)

    Again, i'll repeat, if you use "any" you'll also get Drafts and Revisions, amongst other things in the result set.

    Attachments, should you want to query for them specifically, have a post_type of inherit. The actual post type is inheritted from the attachments parent, which is usally the post it has been attached/inserted into, if one exists...

Reply

You must log in to post.

About this Topic