0

i limit the search in my WP blog to be within post only by adding this line

<?php if (is_search() && ($post->post_type=='page')) continue; ?>

So any results from static pages will be excluded , that fine but i have a problem if i have a post and it has the below content

Test POST  

[video width="320" height="240" mp4="http://localhost/blog/wp-content/uploads/2015/01/a1.mp4" poster="1.jpg"][/video]

 

and if i try to search by this keyword " post " it return this post which has video tag and "poster" attribute , which not make sense so how i can exclude the attribute of media tags in search results .

0

The reason for this is that WordPress searches for matches using LIKE %post%, meaning that any post (that is Post, Page or custom post type object) that contains post in it's title or content is found.

You can alter this default behaviour by using the posts_where action hook.

add_action( 'posts_where', 'my_alter_search_where' );
function my_alter_search_where( $where ){

    global $wpdb;

    if (is_search()) :

        $where.= " AND $wpdb->posts.post_content NOT LIKE '%poster=%'";

    endif;

}

The only caviat here is that if your post content contains both %post and %poster="% as two seperate occurances, it will not be included. There are ways around this, but I'd need to know more about your query, so if this answere is acceptable I'd suggest posting that as a seperate question.

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.