When your website means business.

WP Query | Type: PHPWP_query Sort by Checkbox Field Values

Sorting a query by custom field with values array (checkbox, multi-select, etc). See Catholic Foundation site for implementation.

<!--establish sort values (which coincide with custom field values)-->
<form method="post" action="/giving-options/make-a-donation/" class="fundfilter">
	<div class="sortfunds">Filter funds by designation category:</div>
  <select name="fundcategory" id="fund" class="sortform" onchange="submit();">
    <option <?php echo ($_POST['fundcategory'] == '') ? ' selected="selected"' : ''; ?> value="">All Funds (default)</option>
    <option <?php echo ($_POST['fundcategory'] == 'Education') ? ' selected="selected"' : ''; ?> value="Education">Education</option>
    <option <?php echo ($_POST['fundcategory'] == 'Historic Preservation/Capital Improvement') ? ' selected="selected"' : ''; ?> value="Historic Preservation/Capital Improvement">Historic Preservation/Capital Improvement</option>
    <option <?php echo ($_POST['fundcategory'] == 'Health/Human Services') ? ' selected="selected"' : ''; ?> value="Health/Human Services">Health/Human Services</option>
    <option <?php echo ($_POST['fundcategory'] == 'Parishes/Religious') ? ' selected="selected"' : ''; ?> value="Parishes/Religious">Parishes/Religious</option>
    <option <?php echo ($_POST['fundcategory'] == 'Unrestricted') ? ' selected="selected"' : ''; ?> value="Unrestricted">Unrestricted</option>
  </select>
</form>
	
<?php
	$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; 
	if ((!isset($_POST['fundcategory'])) || ('' == $_POST['fundcategory'])) {
	$fundlist= new WP_Query( array( 
		'post_type' => 'funds',
		'orderby' => 'title',
		'order' => 'ASC',
		'posts_per_page' => 12,
		'paged' => $paged
	) ); 
	} elseif (isset($_POST['fundcategory'])) {
	$category = $_POST['fundcategory'];
	$fundlist= new WP_Query( array(
	'post_type' => 'funds', 
	'posts_per_page' => -1, //keeps paging from resetting filter after page 1
	'orderby' => 'title',
	'order'   => 'ASC',
	'meta-value' => $category,
	'meta_query' => array(
		array (
		'key' => 'fund_category',
                'value' => $_POST['fundcategory'],
		'compare' => 'LIKE',
                'type' => 'CHAR'
		)
	)
) ); 
}
?>

<?php if ($fundlist->have_posts()) :
	if ((!isset($_POST['fundcategory'])) || ('' == $_POST['fundcategory'])) {
	echo '<h2>All Funds</h2>';	
	} elseif ($category) {
	echo '<h2>'.$category.' Funds</h2>';
	}
?>
<table>
	<thead>
    <tr>
      <th width="35%">Fund</th>
      <th width="35%">Designation</th>
      <th width="30%">Category</th>
    </tr>
	</thead>
	<?php while ( $fundlist->have_posts() ) : $fundlist->the_post(); ?>
	<?php
		$fundcat = get_field('fund_category');
		$designate = get_field('fund_designation');
	?>
<tr>
	<td data-label="Fund"><a href="/giving-options/make-a-donation/donate?designation=<?php the_title(); ?>"><strong><?php the_title(); ?></strong></a></td>
	<td data-label="Designation"><?php echo $designate; ?></td>
	<td data-label="Category"><?php
		if ($fundcat) {
			foreach($fundcat as $fund) {
			echo '<div>'.$fund.'</div>';
			}
		}
	?></td>
</tr>
<?php endwhile; ?>
</table> 
<?php
  	else:
		echo '<p><em>There are no '.$category.' funds to display.</em></p>';
		endif;
?>
<?php wp_pagenavi( array( 'query' => $fundlist ) ); ?>
<? wp_reset_query(); ?>
Menu
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Cookies Notice