-
-
Save jchristopher/9dab9a9db6e3ce8c057808fbebf7b75d to your computer and use it in GitHub Desktop.
SearchWP query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' => __( '«' ), | |
'next_text' => __( '»' ), | |
) ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment