How to search for post content and attached file names

If you look at the $wpdb->posts table in the database, you will notice that the file names, minus the file type ending, are used for the post_title for attachments. This means that you can effectively search the file name if you can get your search function to search attachments, which the default search (almost) already does.

In your code, these two lines are working against you…

$search_query['post_type'] = array('post', 'page');
$search_query['post_status'] = 'publish';

… because attachments are 'post_type' == 'attachments' and 'post_status' == 'inherit'. If you alter the second of those to be …

$search_query['post_status'] = array('publish','inherit);

… and leave off the first, you should have it working. That is…

$search_query['s'] = 'annual report';
$search_query['post_status'] = array('publish','inherit');
$search = new WP_Query($search_query);
var_dump($search);

You can also alter the global search with…

function search_attach_names_wpse_99832($qry) {
  if($qry->is_search()) {
    $qry->set('post_status',array('publish','inherit'));
  }
}
add_action('pre_get_posts','search_attach_names_wpse_99832');

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)