Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Forked from anonymous/swp-query.php
Last active September 14, 2016 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/9dab9a9db6e3ce8c057808fbebf7b75d to your computer and use it in GitHub Desktop.
Save jchristopher/9dab9a9db6e3ce8c057808fbebf7b75d to your computer and use it in GitHub Desktop.
SearchWP query
<?php
// retrieve search variables from url
foreach ( $_GET as $key => $value ) {
$$key = sanitize_text_field( $value );
}
// retrieve our search query if applicable
$query = isset( $_REQUEST['swpquery'] ) ? sanitize_text_field( $_REQUEST['swpquery'] ) : '';
// retrieve our pagination if applicable
$swppg = isset( $_REQUEST['swppg'] ) ? absint( $_REQUEST['swppg'] ) : 1;
$args = array(
's' => $s
'page' => $swppg,
);
// If author has been selected include it in the search query
if ( isset( $a ) && $a != 'Select' ) {
$value = str_replace( '+', ' ', $a );
$args['meta_query'] = array(
array(
'key' => 'author',
'value' => $a,
'compare' => 'LIKE',
),
);
}
// If author has been selected include it in the search query
if ( isset( $p ) && $p != 'Select' ) {
$term = strtolower( $p );
$args['tax_query'] = array(
array(
'taxonomy' => 'pubcode',
'field' => 'slug',
'terms' => $term,
),
);
}
// If dateto and datefrom has been selected include it in the search query
if ( $datefrom && $dateto ) {
$datefrom = explode( '/', $datefrom );
$dateto = explode( '/', $dateto );
$args['date_query'] = array(
array(
'after' => array(
'year' => $datefrom[2],
'month' => $datefrom[1],
'day' => $datefrom[0],
),
'before' => array(
'year' => $dateto[2],
'month' => $dateto[1],
'day' => $dateto[0],
),
'inclusive' => true,
),
);
}
/**
* Use SearchWP's SWP_Query to perform a search using the default engine
*/
if ( class_exists( 'SWP_Query' ) && ! empty( $args['s'] ) ) {
$query = new SWP_Query( $args);
} else {
$query = new WP_Query( $args);
}
// set up pagination
$pagination = paginate_links( array(
'format' => '?swppg=%#%',
'current' => $swppg,
'total' => $query->max_num_pages,
'prev_text' => __( '&laquo;' ),
'next_text' => __( '&raquo;' ),
) );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment