Skip to content

Instantly share code, notes, and snippets.

@arvedsen
Created February 28, 2020 11:52
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 arvedsen/d423cb83a6a8d53ca7de91bd4f5601bc to your computer and use it in GitHub Desktop.
Save arvedsen/d423cb83a6a8d53ca7de91bd4f5601bc to your computer and use it in GitHub Desktop.
PMPro Filter member directory multiple select field error
/* The field added via Register Helper. Field works fine in the checkout. */
$fields[] = new PMProRH_Field(
'category', // input name, will also be used as meta key
'select2', // type of field
array(
'label' => 'Vælg interesser', // custom field label
'options' => array("model"=>"Model", "statist"=>"Statist", "actor_unex"=>"Skuespil (uøvet)", "actor_ex"=>"Skuespil (øvet)"), // input size
'class' => 'category', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
'levels' => array(2, 3, 4), // only level 2, 3 and 4 should have the category field
'addmember' => true, // Add field to Admin Add member
'memberslistcsv' => true // include field in csv export
)
);
/* Create filter inputs in sidebar */
<li>
<strong>Kategori:</strong><br/>
<?php
// Set up values to filter for.
$category = array(
"model"=>"Model", "statist"=>"Statist", "actor_unex"=>"Skuespil (uøvet)", "actor_ex"=>"Skuespil (øvet)"
);
foreach ( $category as $key => $value ) {
// Check if this value should default to be checked.
$checked_modifier = '';
if ( ! empty( $_REQUEST['category'] ) && in_array( $key, $_REQUEST['category'] ) ) {
$checked_modifier = ' checked';
}
// Add checkbox.
echo '<input type="checkbox" name="category[]" value="' . $key . '"' . $checked_modifier . '><label> ' . $value . '</label><br/>';
}
?>
</li>
/* Filter results based on category */
if ( ! empty( $_REQUEST['category'] ) && is_array( $_REQUEST['category'] ) ) {
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_category ON um_category.meta_key = 'category' AND u.ID = um_category.user_id ";
$sql_parts['WHERE'] .= " AND um_category.meta_value in ('" . implode( "','", $_REQUEST['category'] ) . "') ";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment