Ajax30 Posted October 19, 2014 Share Posted October 19, 2014 Hello! My name is Razvan and I work as a (front-end) web developer in Bucharest, Romania. I am using the Simple Staff List Word Press plugin on THIS page. I have used the guidance here to sort staff members by full name. I need to order them by last name. For this, even though I am new to PHP, I have written the code below: <?php $name = "Firstname Middlename Lastname"; $name_parts = explode(' ',$name); $no_of_parts = sizeof($name_parts); $last_name_index = $no_of_parts - 1; $last_name = $name_parts[$last_name_index]; $first_names = chop($name,$last_name); $reverse_name = $last_name . ' ' . $first_names; ?> It is made to move the last name at the beginning of the full name, store the result in the $reverse_name, and then sort by $reverse_name. It does move the last name at the beginning of the full name, but I can't do this 2 things: 1) Use the value of the staff member post title as value for the $name variable; 2) Use $reverse_name for sorting. Can anyone give me a hint on what to change in the plugin files to achieve this goal, please? Thank you! Razvan Zamfir Quote Link to comment https://forums.phpfreaks.com/topic/291928-simple-staff-list-word-press-plugin-order-staff-members-by-last-name/ Share on other sites More sharing options...
QuickOldCar Posted October 19, 2014 Share Posted October 19, 2014 (edited) Here is how you can reverse the first and last names. <?php $name = "Firstname Middlename Lastname"; $name_parts = array(); $name_parts = explode(' ',trim($name)); $name_parts = array_reverse($name_parts); $reverse_name = implode(" ",$name_parts); echo $reverse_name; ?> Edited October 19, 2014 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/291928-simple-staff-list-word-press-plugin-order-staff-members-by-last-name/#findComment-1494187 Share on other sites More sharing options...
QuickOldCar Posted October 19, 2014 Share Posted October 19, 2014 An easier way is to use this reverse code just gave on the $_POST["_staff_member_title"] at line 52 in admin-save-data.php Then use the same reverse code for display purposes (normal names) <?php /* // Save Post Data //////////////////////////////*/////// Register and write the AJAX callback function to actually update the posts on sort. ///// add_action( 'wp_ajax_staff_member_update_post_order', 'sslp_staff_member_update_post_order' ); function sslp_staff_member_update_post_order() { global $wpdb; $post_type = $_POST['postType']; $order = $_POST['order']; /** * Expect: $sorted = array( * menu_order => post-XX * ); */ foreach( $order as $menu_order => $post_id ) { $post_id = intval( str_ireplace( 'post-', '', $post_id ) ); $menu_order = intval($menu_order); wp_update_post( array( 'ID' => $post_id, 'menu_order' => $menu_order ) ); } die( '1' ); } ////// Save Custom Post Type Fields ////// add_action('save_post', 'sslp_save_staff_member_details'); function sslp_save_staff_member_details(){ global $post; if ( !isset( $_POST['sslp_add_edit_staff_member_noncename'] ) || !wp_verify_nonce( $_POST['sslp_add_edit_staff_member_noncename'], 'sslp_post_nonce' ) ) { return; } if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post->ID; $name_parts = array(); $name_parts = explode(' ',trim($_POST["_staff_member_title"])); $name_parts = array_reverse($name_parts); $reverse_name = implode(" ",$name_parts); update_post_meta($post->ID, "_staff_member_bio", $_POST["_staff_member_bio"]); update_post_meta($post->ID, "_staff_member_title", $reverse_name); update_post_meta($post->ID, "_staff_member_email", $_POST["_staff_member_email"]); update_post_meta($post->ID, "_staff_member_phone", $_POST["_staff_member_phone"]); update_post_meta($post->ID, "_staff_member_fb", $_POST["_staff_member_fb"]); update_post_meta($post->ID, "_staff_member_tw", $_POST["_staff_member_tw"]); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/291928-simple-staff-list-word-press-plugin-order-staff-members-by-last-name/#findComment-1494189 Share on other sites More sharing options...
QuickOldCar Posted October 19, 2014 Share Posted October 19, 2014 The author of the plugin used the built in posts system instead of making a new table. If there was a new table can save each first,middle,last names would be able to query and sort by however wanted to. Essentially now you must pull results by post title and will go by first character. Quote Link to comment https://forums.phpfreaks.com/topic/291928-simple-staff-list-word-press-plugin-order-staff-members-by-last-name/#findComment-1494191 Share on other sites More sharing options...
Barand Posted October 20, 2014 Share Posted October 20, 2014 Instead of reversing, this would give more natural result $name = "First Middle Last"; $arr = explode(' ', $name); $bySurname = array_pop($arr) . ', ' . join(' ', $arr); //--> Last, First Middle Quote Link to comment https://forums.phpfreaks.com/topic/291928-simple-staff-list-word-press-plugin-order-staff-members-by-last-name/#findComment-1494253 Share on other sites More sharing options...
Ajax30 Posted October 21, 2014 Author Share Posted October 21, 2014 @QuickOldCar: I have modified admin-save-data.php according to your indications (copy and paste actually). My user-view-show-staff-list.php file sorts by title. It looks like this: <?php function sslp_staff_member_listing_shortcode_func($atts) { extract(shortcode_atts(array( 'single' => 'no', 'group' => '', 'wrap_class' => '', 'order' => 'ASC', 'orderby' => 'title', ), $atts)); // Get Template and CSS $custom_html = stripslashes_deep(get_option('_staff_listing_custom_html')); $custom_css = stripslashes_deep(get_option('_staff_listing_custom_css')); $default_tags = get_option('_staff_listing_default_tags'); $default_formatted_tags = get_option('_staff_listing_default_formatted_tags'); $output = ''; $group = strtolower($group); $order = strtoupper($order); $staff = ''; $use_external_css = get_option('_staff_listing_write_external_css'); /** * Set up our WP_Query */ $args = array( 'post_type' => 'staff-member', 'posts_per_page' => -1, 'orderby' => $orderby, 'post_status' => 'publish' ); // Check user's 'order' value if ($order != 'ASC' && $order != 'DESC') { $order = 'ASC'; } // Set 'order' in our query args $args['order'] = $order; $args['staff-member-group'] = $group; $staff = new WP_Query( $args ); /** * Set up our loop_markup */ $loop_markup = $loop_markup_reset = str_replace("[staff_loop]", "", substr($custom_html, strpos($custom_html, "[staff_loop]"), strpos($custom_html, "[/staff_loop]") - strpos($custom_html, "[staff_loop]"))); // Doing this so I can concatenate class names for current and possibly future use. $staff_member_classes = $wrap_class; // Prepare to output styles if not using external style sheet if ( $use_external_css == "no" ) { $style_output = '<style>'.$custom_css.'</style>'; } $i = 0; if( $staff->have_posts() ) { $output .= '<div class="staff-member-listing '.$group.'">'; while( $staff->have_posts() ) : $staff->the_post(); if ($i == ($staff->found_posts)-1) { $staff_member_classes .= " last"; } if ($i % 2) { $output .= '<div class="staff-member odd '.$staff_member_classes.'">'; } else { $output .= '<div class="staff-member even '.$staff_member_classes.'">'; } global $post; $custom = get_post_custom(); $name = get_the_title(); $name_slug = basename(get_permalink()); $title = $custom["_staff_member_title"][0]; $email = $custom["_staff_member_email"][0]; $phone = $custom["_staff_member_phone"][0]; $bio = $custom["_staff_member_bio"][0]; if(has_post_thumbnail()){ $photo_url = wp_get_attachment_url( get_post_thumbnail_id() ); $photo = '<img class="staff-member-photo" src="'.$photo_url.'" alt = "'.$title.'">'; }else{ $photo_url = ''; $photo = ''; } if (function_exists('wpautop')){ $bio_format = '<div class="staff-member-bio">'.wpautop($bio).'</div>'; } $email_mailto = '<a class="staff-member-email" href="mailto:'.antispambot( $email ).'" title="Email '.$name.'">'.antispambot( $email ).'</a>'; $email_nolink = antispambot( $email ); $accepted_single_tags = $default_tags; $replace_single_values = array($name, $name_slug, $photo_url, $title, $email_nolink, $phone, $bio); $accepted_formatted_tags = $default_formatted_tags; $replace_formatted_values = array('<h3 class="staff-member-name">'.$name.'</h3>', '<h4 class="staff-member-position">'.$title.'</h4>', $photo, $email_mailto, $bio_format); $loop_markup = str_replace($accepted_single_tags, $replace_single_values, $loop_markup); $loop_markup = str_replace($accepted_formatted_tags, $replace_formatted_values, $loop_markup); $output .= $loop_markup; $loop_markup = $loop_markup_reset; $output .= '</div> <!-- Close staff-member -->'; $i += 1; endwhile; $output .= "</div> <!-- Close staff-member-listing -->"; } wp_reset_query(); $output = $style_output.$output; return $output; } add_shortcode('simple-staff-list', 'sslp_staff_member_listing_shortcode_func'); ?> What should I modify in the code above to make sorting by $reverse_name work? (As I have mentioned, I am really new to PHP ) Quote Link to comment https://forums.phpfreaks.com/topic/291928-simple-staff-list-word-press-plugin-order-staff-members-by-last-name/#findComment-1494302 Share on other sites More sharing options...
Ajax30 Posted October 21, 2014 Author Share Posted October 21, 2014 An easier way is to use this reverse code just gave on the $_POST["_staff_member_title"] at line 52 in admin-save-data.php Then use the same reverse code for display purposes (normal names) <?php /* // Save Post Data //////////////////////////////*/////// Register and write the AJAX callback function to actually update the posts on sort. ///// add_action( 'wp_ajax_staff_member_update_post_order', 'sslp_staff_member_update_post_order' ); function sslp_staff_member_update_post_order() { global $wpdb; $post_type = $_POST['postType']; $order = $_POST['order']; /** * Expect: $sorted = array( * menu_order => post-XX * ); */ foreach( $order as $menu_order => $post_id ) { $post_id = intval( str_ireplace( 'post-', '', $post_id ) ); $menu_order = intval($menu_order); wp_update_post( array( 'ID' => $post_id, 'menu_order' => $menu_order ) ); } die( '1' ); } ////// Save Custom Post Type Fields ////// add_action('save_post', 'sslp_save_staff_member_details'); function sslp_save_staff_member_details(){ global $post; if ( !isset( $_POST['sslp_add_edit_staff_member_noncename'] ) || !wp_verify_nonce( $_POST['sslp_add_edit_staff_member_noncename'], 'sslp_post_nonce' ) ) { return; } if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post->ID; $name_parts = array(); $name_parts = explode(' ',trim($_POST["_staff_member_title"])); $name_parts = array_reverse($name_parts); $reverse_name = implode(" ",$name_parts); update_post_meta($post->ID, "_staff_member_bio", $_POST["_staff_member_bio"]); update_post_meta($post->ID, "_staff_member_title", $reverse_name); update_post_meta($post->ID, "_staff_member_email", $_POST["_staff_member_email"]); update_post_meta($post->ID, "_staff_member_phone", $_POST["_staff_member_phone"]); update_post_meta($post->ID, "_staff_member_fb", $_POST["_staff_member_fb"]); update_post_meta($post->ID, "_staff_member_tw", $_POST["_staff_member_tw"]); } ?> I have modified admin-save-data.php according to your indications (copy and paste actually), but I probably have to modify other files too, because it does not sort by $reverse_name. See the replay above, please... Quote Link to comment https://forums.phpfreaks.com/topic/291928-simple-staff-list-word-press-plugin-order-staff-members-by-last-name/#findComment-1494320 Share on other sites More sharing options...
QuickOldCar Posted October 21, 2014 Share Posted October 21, 2014 Is it you that adds the team members? Can't you just insert the users names as Last, First Middle? Sorting the title ASC order should work fine. To change the order 'order' => 'ASC', could be 'order' => 'DESC', Realize that what I proposed will have to reverse all the current names have in database to be last,middle,first, same with permalinks. Then just for display purposes in your gallery you can reverse them again for the post_title so look like normal names. (or can do as barand suggested and leave them as Last, First Middle, no reverse on display required) example of your link now http://testing.merchantlaw.com/team/casey-r-churko/ should be http://testing.merchantlaw.com/team/churko-r-casey/ ASC order for the title should work if the last names in the database are last,middle,first. You may want to do new posts or change them all in wordpress including the pretty permalinks. Quote Link to comment https://forums.phpfreaks.com/topic/291928-simple-staff-list-word-press-plugin-order-staff-members-by-last-name/#findComment-1494348 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.