Tag Archives: query

Heres a little WordPress snippet that allows you to exclude the current WordPress post from the WP Query – i.e. list of recent posts, more posts from the same category, etc. What the snippet does, is getting the ID of the current post and then use this post ID to exclude the current post form being included in the query.

Instructions
Add this code to your WordPress theme template files where you usually use your queries.

<?php
     $currentID = get_the_ID(); // get current post ID and convert it into a variable
     $my_query = new WP_Query(
          array('cat' => '1',
          'showposts' => '5',
          'post__not_in' => array(
               $currentID))
          );
     while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
          <h1><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h1>
          <?php the_content(); ?>
     <?php endwhile; ?>

Custom post types are not included in the WordPress search results by default – adding this snippet to your functions.php creates a function that allows specific custom post types to be queried and searched by WordPress.

Instructions
Add this to your functions.php file

add_filter( 'pre_get_posts', 'custom_post_type_search' );
function custom_post_type_search( $query ) {
     if ($query->is_search) {
          $query->set('post_type', array( 'post', 'mycustomposttype1', 'mycustomposttype2', 'mycustomposttype3'));
     }
     return $query;
}

This snippet will automatically include all public post types (that have not been excluded from searches), in your WordPress search results.

Instructions
Add this to your functions.php file

function wpsnippet_include_post_types_in_search($query) {
	if(is_search() && is_main_query()) {
		$post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'objects');
		$searchable_types = array();
		if($post_types) {
			foreach( $post_types as $type) {
				$searchable_types[] = $type->name;
			}
		}
		$query->set('post_type', $searchable_types);
	}
	return $query;
}
add_action('pre_get_posts', 'wpsnippet_include_post_types_in_search');

This snippet will allow you to control which posts or pages there should be excluded from WordPress search results.

Instructions
Add this code to your functions.php file.
* In this snippet, posts with the IDs 0 and 1 will be excluded – edit accordingly to suit your needs.

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('cat','0,1');
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');

Heres a little nifty WordPress snippet that will show the number of queries made and the page load time. This can come in handy if you, for instance, need to debug or optimize the performance of your WordPress site.

Instructions
Add this code snippet to a template file during testing in the location where you want the info to show. In this example the output is wrapped in an “if user is administrator” condition – so that the output is only visible to users with administrator rights.

<?php if(current_user_can('administrator')): ?>
     <?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.
<?php endif; ?>

Related links
http://www.livexp.net/wordpress/display-wordpress-page-loading-time-and-number-of-queries.html