WordPress – Exclude from Search Results

Let’s say you have a WordPress site (WP is used here more as a CMS rather than a blogging platform) where all content is created using the Pages as opposed to the Posts. However, the Posts (blog functionality) are also used, but rather for notifications, news, etc.
This is one of the cases where you might need to exclude all Posts from the site search.
[ad]

So how would we do that?
There is definitely more ways on how to exclude Posts or Pages from WordPress search results, but this one is particularly simple with the use of the get_post_type() function.
In the theme’s search.php file, after the start of the loop:

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

add:

<?php if (get_post_type() == 'post') continue; ?>

this would obviously be different for each theme, but the complete search.php could look like this:

<?php get_header(); ?>
<div id="content" class="narrowcolumn" role="main">
<?php if (have_posts()) : ?>
<h2 class="pagetitle">Search Results</h2>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
</div>
 
<?php while (have_posts()) : the_post(); ?>
 
<?php if (get_post_type() == 'post') continue; ?>
 
<div <?php post_class() ?>>
<h3 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h3>
<small><?php the_time('l, F jS, Y') ?></small>
<p class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?> 
Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> 
<?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?>
</p>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
</div>
<?php else : ?>
<h2 class="center">No posts found. Try a different search?</h2>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

possibly you might want to exclude few of the Pages as well by listing the page IDs in an array:

<?php if (get_post_type() == 'post') continue; ?>
<?php if( in_array($post->ID, array('410','412','575','8','156') ) ) continue; ?>

That’s it. Hope this helps someone…
[ad]

WordPress – Exclude from Search Results

Let’s say you have a WordPress site (WP is used here more as a CMS rather than a blogging platform) where all content is created using the Pages as opposed to the Posts. However, the Posts (blog functionality) are also used, but rather for notifications, news, etc.
This is one of the cases where you might need to exclude all Posts from the site search.
[ad]

So how would we do that?
There is definitely more ways on how to exclude Posts or Pages from WordPress search results, but this one is particularly simple with the use of the get_post_type() function.
In the theme’s search.php file, after the start of the loop:

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

add:

<?php if (get_post_type() == 'post') continue; ?>

this would obviously be different for each theme, but the complete search.php could look like this:

<?php get_header(); ?>
<div id="content" class="narrowcolumn" role="main">
<?php if (have_posts()) : ?>
<h2 class="pagetitle">Search Results</h2>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
</div>
 
<?php while (have_posts()) : the_post(); ?>
 
<?php if (get_post_type() == 'post') continue; ?>
 
<div <?php post_class() ?>>
<h3 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h3>
<small><?php the_time('l, F jS, Y') ?></small>
<p class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?> 
Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> 
<?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?>
</p>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
</div>
<?php else : ?>
<h2 class="center">No posts found. Try a different search?</h2>
<?php get_search_form(); ?>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

possibly you might want to exclude few of the Pages as well by listing the page IDs in an array:

<?php if (get_post_type() == 'post') continue; ?>
<?php if( in_array($post->ID, array('410','412','575','8','156') ) ) continue; ?>

That’s it. Hope this helps someone…
[ad]

Redirect WordPress Pages – 301 Redirect Template Download

This page template is designed to provide us with a simple and fast way to redirect WordPress pages using 301 seo friendly redirects. This approach is based on using Custom Fields and a specific Page Template, aiming to make dealing with page redirects as simple as possible.

Originally I created this redirect template prior the WP3 era when needed to have multiple links (with different names) to the same page in the menu. Now since WP3 this can be solved with Custom Menus using the wp_nav_menu() function where links within the menu are created manually and multiple links to the same page or even external links are not an issue anymore. But in cases when we want to display a link to a new page in the menu as soon as it was published, using the classic WP menus does makes more sense as we don’t need to create the links manually.

You can download the template here:

[download url=’http://docs.moonpixel.com/100919-page-redirect.zip’]
[ad]

Here are simple steps on how to use this 301 redirect template:

  1. Download the redirect template, unzip and upload into your WP theme.
    [ad]
  2. On the right hand side in your Attributes box, select the Redirect Page for the page you wish to redirect.

  3. Create a new custom field called redirect in your Custom Fields box below the main post content edit field, and give it the link you wish redirect to as a value.

    alternatively you can use the Page ID instead of the URL, which is a better solution as when the URL changes the redirect will still work.

    That’s all there is to it. Happy redirecting.