Skip to content

Instantly share code, notes, and snippets.

@wpmark
Last active October 14, 2016 08:11
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 wpmark/7edc5bf6b2fb8297082b to your computer and use it in GitHub Desktop.
Save wpmark/7edc5bf6b2fb8297082b to your computer and use it in GitHub Desktop.
<?php
if ( ! isset( $_SESSION ) )
session_start();
function pxjn_init_session_variables() {
if( ! isset( $_SESSION[ 'pxjn_wpac_job_category' ] ) )
$_SESSION[ 'pxjn_wpac_job_category' ] = 'zero';
if( ! isset( $_SESSION[ 'pxjn_wpac_job_location' ] ) )
$_SESSION[ 'pxjn_wpac_job_location' ] = 'zero';
if( ! isset( $_SESSION[ 'pxjn_wpac_job_type' ] ) )
$_SESSION[ 'pxjn_wpac_job_type' ] = 'zero';
}
add_action( 'init', 'pxjn_init_session_variables', 10 );
function pxjn_init_job_search_form() {
/* check form is submitted */
if( $_POST[ 'pxjn_search_form_used' ] == 'yes' ) {
if( isset( $_POST[ 'pxjn_wpac_job_category' ] ) )
$_SESSION[ 'pxjn_wpac_job_category' ] = (int) $_POST[ 'pxjn_wpac_job_category' ];
if( isset( $_POST[ 'pxjn_wpac_job_location' ] ) )
$_SESSION[ 'pxjn_wpac_job_location' ] = (int) $_POST[ 'pxjn_wpac_job_location' ];
if( isset( $_POST[ 'vwpac_job_type' ] ) )
$_SESSION[ 'pxjn_wpac_job_type' ] = (int) $_POST[ 'pxjn_wpac_job_type' ];
}
}
add_action( 'init', 'pxjn_init_job_search_form', 20 );
function pxjn_job_search_alter_query( $query ) {
if( ! $query->is_main_query() || is_admin() || ! $query->is_post_type_archive( 'wpac_job' ) )
return;
/* setup a tax query array to store our tax queries in including an and relation to add tax_queries together */
$pxjn_tax_query = array( 'relation' => 'AND' );
/* setup an array to store our meta queries in */
$pxjn_meta_query = array();

/* check if user has chosen a job category to search within */
if( $_SESSION[ 'pxjn_wpac_job_category' ] != 'zero' ) {
$pxjn_tax_query[] = array(
'taxonomy' => 'wpac_job_category',
'field' => 'id',
'terms' => (int) $_SESSION[ 'pxjn_wpac_job_category' ]
);
} // end if have job cat set
/* check if user has chosen a job location to search within */
if( $_SESSION[ 'pxjn_wpac_job_location' ] != 'zero' ) {
$pxjn_tax_query[] = array(
'taxonomy' => 'wpac_job_location',
'field' => 'id',
'terms' => (int) $_SESSION[ 'pxjn_wpac_job_location' ]
);
} // end if have job location set
/* check if user has chosen a job type to search within */
if( $_SESSION[ 'pxjn_wpac_job_type' ] != 'zero' ) {
$pxjn_tax_query[] = array(
'taxonomy' => 'wpac_job_type',
'field' => 'id',
'terms' => (int) $_SESSION[ 'pxjn_wpac_job_type' ]
);
} // end if have job type set
/* check whether we should be running a tax_query */
if( ! empty( $pxjn_tax_query ) ) {
$query->set( 'tax_query', $pxjn_tax_query );
} // end if need tax query
return;
}
add_action( 'pre_get_posts', 'pxjn_job_search_alter_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment