Sort by category with title
-
I would like to show all places from one category and then from another, preferably with a title above the categories in the list of places. Is this possible?
The page I need help with: [log in to see the link]
-
Hi there,
First of all check out this article on how to show the category names in the search results.
With this, you will also have the category names in the store metadata that is sent to the frontend, so you could also use this method to change the sort order, choosing the key ‘terms’ for sorting in this case.
Then you would have to modify the results template to show the category name on top of the rest of the store details.
Then you would need to hide the category name for stores of a certain category other than the first one, so in practice the category name shows up first, and then all the stores belonging in that category come after that, I’ll try to figure out a way to do this as I cannot think of any right now.
I hope that helps.
Hi! Thanks so much! I was able to show the category underneath the address, and sort them in the right order. Only seems to be working when logged in for now, but probably a cache issue.
I can’t seem to figure out how to show the category name above the results though, so let me know when you’ve figured something out!
Hi again,
We actually haven’t come up with a way to show the category name just once, then show all the stores belonging in that category, then showing a second category name, then the stores in that category, and so on, so maybe you’ll have to stick with your sort order “by category”, and just show the category name in every store.
As for the issue you mention where you cannot show the category name above the results, all you have to do is modify the template of the search results with the wpsl_listing_template and positon the <%= terms %> output wherever you see fit ??
I hope that helps.
Hi again,
We’ve been giving it a thought, and maybe you can try this to show the category name just once, followed by all the stores belongning in that category.
You will have to access the search results before they are returned to the frontend. Use this filter for that.
Loop over the array and regbuild it grouped by term, and finally merge it in a new array.
Let us know if that points you in the right direction.
i want to show the category name just once, followed by all the stores belongning in that category. but i cannot figureout how to do that by coding. i wrote the filter but don’t know what to write in the loop. Please help
Hi again,
Sorry for the late reply. I am going to send you a code snippet that would roughly accomplish what you are looking for. As you can see, it involves a lot of filters, because this is a functionality that was not originally planned for the plugin, but in any case it can be done with some effort:
/** * Save category names in the store_meta array */ add_filter( 'wpsl_store_meta', 'custom_store_meta', 10, 2 ); function custom_store_meta( $store_meta, $store_id ) { $terms = get_the_terms( $store_id, 'wpsl_store_category' ); $store_meta['terms'] = ''; if ( $terms ) { if ( ! is_wp_error( $terms ) ) { if ( count( $terms ) > 1 ) { $location_terms = array(); foreach ( $terms as $term ) { $location_terms[] = $term->name; } $store_meta['terms'] = implode( ', ', $location_terms ); } else { $store_meta['terms'] = $terms[0]->name; } } } return $store_meta; } /** * Access the search results just before they are returned. * Loop over the array and rebuild it grouped by category, and * finally merge this in a new array. */ add_filter( 'wpsl_store_data', 'custom_store_data' ); function custom_store_data( $store_data ) { $grouped_data = array(); // group results by category foreach ( $store_data as $k => $store ) { $grouped_data[$store['terms']][] = $store; } // rebuild the output array with the elements of each group of categories // we create a new 'category_heading' element whenever we need to output a new category heading $output = array(); foreach ( $grouped_data as $category_name => $group ) { $category_heading = ''; foreach ( $group as $k => $store ) { if ( $category_heading != $category_name ) { $store['category_heading'] = $category_name; $category_heading = $category_name; } else { $store['category_heading'] = ''; } $output[] = $store; } } return $output; } /** * Include the new category_heading field data in the JSON Response */ add_filter( 'wpsl_frontend_meta_fields', 'custom_frontend_meta_fields' ); function custom_frontend_meta_fields( $store_fields ) { $store_fields['wpsl_category_heading'] = array( 'name' => 'category_heading' ); return $store_fields; } /** * Modify our store listing template to display the new categoryt field, if exists */ add_filter( 'wpsl_listing_template', 'custom_listing_template' ); function custom_listing_template() { global $wpsl, $wpsl_settings; $listing_template = '<li data-store-id="<%= id %>">' . "\r\n"; /* * Show new 'category_heading' field that we just created, if it exists */ $listing_template .= "\t\t\t" . '<% if ( category_heading ) { %>' . "\r\n"; $listing_template .= "\t\t\t" . '<h4 style="color:red"><%= category_heading %></h4>' . "\r\n"; $listing_template .= "\t\t\t" . '<% } %>' . "\r\n"; /* * Show the rest of the info */ $listing_template .= "\t\t" . '<div class="wpsl-store-location">' . "\r\n"; $listing_template .= "\t\t\t" . '<p><%= thumb %>' . "\r\n"; $listing_template .= "\t\t\t\t" . wpsl_store_header_template( 'listing' ) . "\r\n"; // Check which header format we use $listing_template .= "\t\t\t\t" . '<span class="wpsl-street"><%= address %></span>' . "\r\n"; $listing_template .= "\t\t\t" . '</p>' . "\r\n"; $listing_template .= "\t\t" . '</div>' . "\r\n"; $listing_template .= "\t" . '</li>'; return $listing_template; }
Try it out and let us know if it doesn’t work for you.
Regards.
- The topic ‘Sort by category with title’ is closed to new replies.