My website has an archive page with all of our team members on it. There's a filter that allows users to segment the team members alphabetically by the first letter of the "last_name" field. We also have a "nickname" field that we'd like included in the filter so when a letter is selected, results also pull in from the "nickname" field in addition to the "last_name" field.
How do I change the code below to accomplish this?
How do I change the code below to accomplish this?
Code:
wp_reset_postdata();
global $wpdb;
$additional_joins = "";
$additional_queries = "";
$your_search = "";
$additional_joins .= " INNER JOIN $wpdb->postmeta as tbl_last_name on $wpdb->posts.ID = tbl_last_name.post_id and tbl_last_name.meta_key = 'last_name'";
$additional_joins .= " LEFT JOIN $wpdb->postmeta as tbl_nickname on $wpdb->posts.ID = tbl_nickname.post_id and tbl_nickname.meta_key = 'nickname'";
if (!empty($_GET['letter'])) {
$your_search .= "Last Name starting with: <b>" . esc_html__($_GET['letter']) . "</b>";
$additional_queries .= " and (tbl_last_name.meta_value like '" . sanitize_text_field($_GET['letter']) . "%')";
}
if (!empty($_GET['who'])) {
$your_search .= (!empty($your_search) ? ", " : "") . "Name: <b>" . stripslashes(esc_html__($_GET['who'])) . "</b>";
$who_array = explode(" ",$_GET['who']);
foreach ($who_array as $name_part) {
$additional_queries .= " and (post_title like '%" . sanitize_text_field($name_part) . "%' or tbl_nickname.meta_value like '%" . sanitize_text_field($name_part) . "%')";
}
}