2

I have a page where I show all the attachments of a page in a table which has id=10.

I have hundreds of attachments and I would like to implement an AJAX search form that uses the title of attachment as key for search.

Actually the problem is not in implementing the AJAX call, but the query that returns the attachments list.

2

You should try applying the s(search) as parameter for your custom query.

See this example here:

$query = new WP_Query( 's=keyword' );

and you can then apply normal loop to iterate through your results. This also performs string match same as like operator %keyword% you mentioned in comment for @Ravs' answer.

Refer documentation here.

| improve this answer | |
2

WP_Query will not do what you want by default. If you try to search by post_name you will get a post_name= query which is too strict. If you use the s parameter you will get a %term% search over both the post name and the post content and term will match anywhere in the word, not just at the beginning. And that is too broad for what you are doing. And s becomes increasingly less efficient as you add terms, though that may not be an issue for your.

You say you want a match if you tyoue the "first n" letters, so you need to filter the query. This should do it.

function search_filter_right_wild($search) {
  remove_filter('posts_where','search_filter_right_wild');
  global $wpdb;
  $pattern = "|{$wpdb->posts}.post_name = '([^']*)'|";
  $search = preg_replace($pattern,"{$wpdb->posts}.post_name LIKE '$1%'",$search);
  return $search;
}
add_filter('posts_where','search_filter_right_wild');

$q = new WP_Query( 
  array(
    'name' => 'test',
    'post_type' => 'attachment', 
    'numberposts' => -1, 
    'post_status' => 'inherit'
  ) 
);
var_dump($q->request);

You are searching the post name only and the filter adds a % to the right only so the wildcard match is only on right of the term.

Add the filter in your AJAX callback before the query and it will remove itself automatically (though that last part probably is not necessary).

| improve this answer | |
2

you query in may be like

<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' =>'any', 'post_parent' => 10 ); 
$attachments = get_posts($args);
if ($attachments) {
    foreach ( $attachments as $attachment ) {
        if('you title of image'==$attachment->post_title ){
              //your code goes here
        }
    }
}
?>

Important Link:

get_posts()

Ajax Search Form

| improve this answer | |
  • Thank you Ravs! But here we have: if('you title of image'==$attachment->post_title ){ //your code goes here } So what i write in the search input should be == to attachment name...My case is a bit different: If i have an attachement named: salvatore.txt i would like to get it even if i write the first two letter: sa. It is the same system used on google search... – Salvatore Dibenedetto Jun 7 '13 at 10:51
  • @SalvatoreDibenedetto i updated my answer,go threw link below get_post() in important link that is ajax search form – Ravinder Kumar Jun 7 '13 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.