Skip to content

Instantly share code, notes, and snippets.

@cameronbaney
Last active December 11, 2015 21:58
Show Gist options
  • Save cameronbaney/4665651 to your computer and use it in GitHub Desktop.
Save cameronbaney/4665651 to your computer and use it in GitHub Desktop.
Create A Filter Based On Query Strings For Relationships
<?php
//***********************************************************/
// CREATE A FILTER BASED ON QUERY STRINGS FOR RELATIONSHIPS //
// URL FOR THIS EXAMPLE: /?practice_area=92,88&lawyer=69 //
//***********************************************************/
// Get the query string for practice areas
$pas = explode(',', $_GET['practice_area']);
$lawyers = explode(',', $_GET['lawyer']);
// Set the original variable for the query
$meta_query = array();
// Check if the user is trying to get practice areas
if (isset($_GET['practice_area'])) {
foreach($pas as $panum){
$pID = '"' . $panum . '"';
$meta_query[] = array('key' => 'rel_practice_area','value' => "$pID",'compare' => 'LIKE');
}
}
// Check if the user is trying to get lawyers
if (isset($_GET['lawyer'])) {
foreach($lawyers as $lnum){
$lID = '"' . $lnum . '"';
$meta_query[] = array('key' => 'rel_attorney','value' => "$lID",'compare' => 'LIKE');
}
}
// Query the relationship - With Pagination.
$relcases = new WP_Query(array('post_type'=>'verdicts','posts_per_page'=>'10','paged'=>get_query_var('paged'),'meta_query'=>$meta_query)
);
// Check if there are any relationships
if($relcases->found_posts){
// The loop
while ( $relcases->have_posts() ) : $relcases->the_post();
get_template_part( 'content', 'verdicts' );
endwhile; ?>
<?php // Pagination ?>
<div class="blog-nav">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $relcases->max_num_pages
) );
?>
</div>
<?php
} else { ?>
<h2>Sorry, nothing was found</h2>
<?php
}
wp_reset_postdata(); // Reset the post
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment