WordPress.org

Ready to get started?Download WordPress

Forums

Search Everything
Filtering the attachments by mime type (6 posts)

  1. karl19
    Member
    Posted 2 years ago #

    I am using the function to search attachments and would like to filter out images from attachments listed in the search results, or alternatively to just search for PDF attachments.

    The site I'm working on has a lot of uploaded PDFs and it'd be useful to get them to show up in the search results (which works fine with Search Everything), but it also lists all images uploaded, which are not so relevant to the site search.

    Would be great if someone knew if this was possible and how to do it.

    http://wordpress.org/extend/plugins/search-everything/

  2. karl19
    Member
    Posted 2 years ago #

    Couldn't figure out a plugin-related and neat way of doing this, but ended up filtering the search results in the loop with something like this:

    if ( get_post_type() == 'attachment' ) {
      if ( get_post_mime_type() == 'application/pdf' ) {

    which works OK for now.

  3. dojwebmaster
    Member
    Posted 2 years ago #

    Karl, thanks a lot for posting this. Unfortunately I have not got the code statement you posted to work in my loop file. When I do a direct copy of your code and inputted it into my loop file - no matter where I put it I get a syntax error. I have copied some of my code below and was wondering if you could give me any recommendations?

    Also, what if I wanted to concatenate Word and Excel files to appear?

    <?php /* LOOP TO DISPLAY POSTS */ ?>

    <?php

    global $wp_query;

    $args = array('post_type' => array('post', 'departments', 'featuredemp'));

    $args = array_merge( $args, array_filter( $wp_query->query_vars ) );

    query_posts($args); ?>

    <?php // NO POSTS TO DISPLAY? ?>

    <?php if ( ! have_posts() ) : ?>

    <p class="intro"><?php echo __('Apologies, but no results were found for the requested archive.
    Perhaps searching will help find a related post.','duotive'); ?>

    <?php endif; ?>

    <?php // HAVE POSTS TO DISPLAY? ?>

    <?php while ( have_posts() ) : the_post(); ?>

    <div class="post blog-classic">

    <h2>

    " title="<?php echo __('Permalink to ', 'duotive').the_title_attribute( 'echo=0' ); ?>" rel="bookmark">

    <?php the_title(); ?>

    </h2>

    <?php if ( has_post_thumbnail() ): ?>

    <?php $thumbnail_src = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>

    " title="<?php the_title();?>">

    <img src="<?php echo get_bloginfo('template_directory');?>/includes/timthumb.php?src=<?php echo $thumbnail_src; ?>&h=180&w=640&zc=1&q=100" alt="<?php the_title(); ?>" />

    <?php endif; ?>

    <?php if(is_search()): ?>

    <?php the_excerpt(); ?>

    <?php else: ?>

    <?php the_content(''); ?>

    " title="<?php the_title();?>"><?php echo __('Read More »','duotive'); ?>

    <?php endif; ?>

    <div class="post-meta">

    <span class="post-date"><?php echo __('posted on ','duotive'); ?><?php the_time('F'); echo ' '; the_time('jS'); echo ' '; the_time('Y');?></span>

    <?php if ( count( get_the_category() ) ) : ?>

    <span class="categories-link">, <?php echo __('Categories:','duotive').' <span class="title">'.get_the_category_list( ', ' ).'</span>'; ?></span>

    <?php endif; ?>

    <?php $tags_list = get_the_tag_list( '', ', ' ); ?>

    <?php if ( $tags_list ): ?>

    <span class="tag-links">

    <?php echo __('Tags:','duotive').' <span class="title">'.$tags_list.'</span>'; ?>

    </span>

    <?php endif; ?>

    <!-- end of post meta -->

    </div>

    <!--end of blog-classic -->

    </div>

    <?php endwhile;?>

    <?php if(function_exists('wp_pagenavi')): ?>

    <div id="navigation">

    <?php wp_pagenavi();?>

    </div>

    <?php endif; ?>

  4. karl19
    Member
    Posted 2 years ago #

    Hi dojwebmaster,

    You should be able to put it right after the loop starts, which is right after:

    <?php while ( have_posts() ) : the_post(); ?>

    Then place:

    if ( get_post_type() == 'attachment' ) {
      if ( get_post_mime_type() == 'application/pdf' ) {

    which essentially says, "if an attachment, then..", and then goes
    through all attachments and says, "if a pdf file..". Could it be that the syntax error you receive is because you don't close these two IFs? At the end of the loop you're checking, before the:

    <?php endwhile; ?>

    you need to close them:

    <?php }
    } ?>

    Hopefully that helps.

  5. dojwebmaster
    Member
    Posted 2 years ago #

    Mucho gracias amigo, looks like I did get it to work... I think? Now with this new construct in the code, will it ONLY return PDF's in the search?

    It appears that what it did for me, meaning that I was only getting PDF's to come back in my search's but not Post or Page data?

    My goal is to have Post, Page and File Attachments but to omit JPG and other image formats.

  6. karl19
    Member
    Posted 2 years ago #

    Then you could probably do something like this:

    if ( get_post_type() == 'attachment' ) {
      if ( get_post_mime_type() == !'image/jpeg' && !'image/png' && !'image/gif' ) {
        //HERE YOU DEFINE WHAT ATTACHMENT DATA YOU WANT TO DISPLAY
      }
    } else {
      // THIS IS THE LOOP FOR ANYTHING ELSE THAN AN ATTACHMENT
    }

    The above should (I hope!) first check if an attachment, then check that it's NOT Jpeg, Gif or PNG, then print whatever data you want to output. If not an attachment, you can print data for anything else.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic