Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to write a function for my functions.php file. I has to do the following;

  • loop through search results and check the template
  • if the template is 'landing.php' add it to ad array
  • use the id's collected in the array to exclude these pages from the search results.

I found some code on the Wordpress Codex forum, I have added a little bit myself but unfortunately don't know what i'm doing...

function filter_where($where = '') {
global $wpdb; // do I need this?
if ( is_search() ) {
    if(is_page_template('landing.php')) { echo 'yes!'; } // ids collected here
    $exclude = array(286);  

    for($x=0;$x<count($exclude);$x++){
      $where .= " AND ID != ".$exclude[$x];
    }
}
return $where;
}
add_filter('posts_where', 'filter_where');
share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

The filter you are using posts_where affects the creation of SQL query, so by definition it is executed before query runs and you have any search results.

So you cannot loop through results at this point, what you can do is retrieve or hardcode list of unwanted items from elsewhere and use them to modify query.

Update

Ok, this is faster to code than trying to solve with comments. This is probably not perfect but should be good starting point for what you want:

add_action('pre_get_posts','exclude_pages');

function exclude_pages( $query ) {

    if( !empty( $query->query_vars['s'] ) ) {

        $pages = get_posts(array(
                            'post_type' => 'page',
                            'meta_query' => array( array(
                                'key' => '_wp_page_template',
                                'value' => 'landing.php',
                            )),
                           ));

        $exclude = array();

        foreach( $pages as $page )
            $exclude[] = $page->ID;

        $query->set('post__not_in', $exclude);
    }
}
share|improve this answer
    
Hi, is it possible to loop through my pages first and get the id's by template, then parse them to this function? –  Dan May 31 '11 at 21:44
    
@Dan I don't think you want to mess with looping at all here. Since template is essentially value in _wp_page_template custom field you can just fetch all pages iwth template directly, using meta query. –  Rarst May 31 '11 at 21:49
    
I'm not sure how that will fit in with the search feature....how about just getting the results and THEN sorting out the template? Or, what about using the filter to not get posts with a certain template? I haven't looking in the database recently to see how they are related - no doubt it won't be simple... –  Dan May 31 '11 at 22:01
    
@Dan there are multiple ways to do this, but none that I can think off are very novice-friendly... Maybe using pre_get_posts hook would be less messy that dealing witg with SQL. –  Rarst May 31 '11 at 22:08
    
Im trying to write some code to test the template in the loop, after I have the returned posts. Do you know what function I need? –  Dan May 31 '11 at 22:10
show 3 more comments

If you need to remove some specific pages or posts from your search results, you can easily do this using the "WP Hide Post" plugin.

According to its description: "Enables you to control the visibility of items on your blog by making posts/pages hidden on some parts of your blog, while still visible in other part."

In particular, this plugin allows you to control the visibility of a post in various different views:

  • The Front Page (Homepage, depending on your theme, this may not be relevant)
  • The Category Page (listing the posts belonging to a category)
  • The Tag Page (listing the posts tagged with a given tag)
  • The Authors Page (listing the posts belonging to an author)
  • The Archive Pages (listing the posts belonging to time period: month, week, day, etc..)
  • The Search Results
  • Feeds

The posts will disappear from the places you choose them to disappear. Everywhere else they will show up as regular posts. In particular, permalinks of the posts still work, and if you generate a sitemap, with something like the Google XML Sitemaps the post will be there as well. This means that the content of your post will be indexed and searchable by search engines.

For a WordPress page, this plugin also allows you to control the visibility with two options:

  • Hide a page on the front page (homepage) only.
  • Hide a page everywhere in the blog (hiding the page in the search results is optional).
share|improve this answer
    
Thanks for the reply, if possible i'd like to do this without a plugin - I'm trying to understand this stuff so I can use it again : ) Im just trying to read up on functions a bit –  Dan May 31 '11 at 17:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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