Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP Need help with archive filter

jday_4

New Coder
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?

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) . "%')";
        }
    }
 
Hiya, if i understand the question right. This might help :-

To include the "nickname" field in the filter for the archive page, you need to modify the code in the following ways:

1. Update the $additional_joins variable to include the join for the "nickname" field:

PHP:
$additional_joins .= " LEFT JOIN $wpdb->postmeta as tbl_nickname on $wpdb->posts.ID = tbl_nickname.post_id and tbl_nickname.meta_key = 'nickname'";


This adds a left join to the postmeta table to include the "nickname" field.

2. Modify the conditions for filtering based on the letter and name:

PHP:
if (!empty($_GET['letter'])) {
    $your_search .= "Last Name or Nickname starting with: <b>" . esc_html__($_GET['letter']) . "</b>";
    
    $additional_queries .= " and ((tbl_last_name.meta_value like '" . sanitize_text_field($_GET['letter']) . "%') or (tbl_nickname.meta_value like '" . sanitize_text_field($_GET['letter']) . "%'))";
}

if (!empty($_GET['who'])) {
    $your_search .= (!empty($your_search) ? ", " : "") . "Name or Nickname: <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) . "%'))";
    }
}


In these updated conditions, the filter checks both the "last_name" field and the "nickname" field for matches. The $additional_queries variable is modified accordingly to include the additional conditions.

By making these changes, the filter will consider both the "last_name" and "nickname" fields when segmenting team members alphabetically or searching by name.
 
Back
Top Bottom