Steven Jones
WordPress Developer in Newcastle upon Tyne, UK.
Steven Jones
@stompweb
function sj_change_post_type_for_search($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('post_type', array('events', 'post'));
}
}
}
add_action('pre_get_posts','sj_change_post_type_for_search');
<input type="hidden" name="post_type" value="events" />
http://www.example.com/?cat=castles
<form action="<?php echo esc_url( home_url( '/' ) ); ?>">
<select name="cat">
<option value="houses">Houses</option>
<option value="flats">Flats</option>
<option value="castles">Castles</option>
</select>
</form>
http://www.example.com/?cms=wordpress
<form action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="radio" name="cms" value="wordpress">WordPress<br/>
<input type="radio" name="cms" value="drupal">Drupal<br/>
<input type="radio" name="cms" value="magento">Magento
</form>
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'colour',
'value' => 'Black',
),
array(
'key' => 'animal',
'value' => 'Cat',
),
),
array(
'relation' => 'AND',
array(
'key' => 'colour',
'value' => 'Brown',
),
array(
'key' => 'animal',
'value' => 'Dog',
),
),
),
function sj_change_order_of_search_results($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('orderby', 'name');
$query->set('order', 'ASC');
}
}
}
add_action('pre_get_posts','sj_change_order_of_search_results');
function sj_redirect_keywords($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
if ('dyson' == santitize_text_field($_GET['s']) {
wp_redirect( home_url('dyson') ); exit;
}
}
}
}
add_action('pre_get_posts','sj_redirect_keywords');
function sj_hide_pages($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$wp_query->set('post__not_in', array( 28 ) );
}
}
}
add_action('pre_get_posts','sj_redirect_keywords');
function sj_change_number_of_results_per_page($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('posts_per_page', 50);
}
}
}
add_action('pre_get_posts','sj_change_number_of_results_per_page');
Running = Runner = Run
<?php
/* Separate the pages and documents into two, separate arrays */
$documents = wp_filter_object_list( $wp_query->posts, array('post_type' => 'documents'));
$pages = wp_filter_object_list( $wp_query->posts, array('post_type' => 'page'));
/* If they exist, print out the documents */
if (!empty($documents)) : ?>
<h2>Documents</h2>
<ul>
<?php foreach($documents as $post) : setup_postdata( $post ); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; wp_reset_postdata(); ?>
</ul>
endif;
/* If they exist, print out the pages */
if (!empty($pages)) : ?>
<h2>Pages</h2>
<?php foreach($pages as $post) : setup_postdata( $post ); ?>
<div class="page">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the excerpt(); ?>
</div>
<?php endforeach; wp_reset_postdata();
endif; ?>
By Steven Jones
WordPress North East - 15th January / WordCamp Birmingham February 6th