Hello,
I currently had to recreate my site, dailydrivenms.com, due to a plugin issue. I'm trying to get a search view functioning again.
I currently have 4 custom post types:
Makes
Models
Platforms
Tuners
These are connected by 3 relationships:
A one-to-many between Makes and Models
A one-to-many between Models and Platforms
A many to many between Platforms and Tuners
There is also a custom field called Tuning Categories. it's a checkboxes field with 6 options, and you can select one or all of them:
-Brakes
-Drivetrain
-Exterior
-Interior
-Power
-Suspension
I set up the view based on the many-to-many post relationship between Platforms and Tuners. it is located at hidden link. It searches the Make, Model, and Platform just fine. It even searches by the category just fine. However, it seems a category is required in order to complete the search. If I set the default value to "All", it will not pull up any search results as "all" is not one of the options. If I set it as blank, it defaults to the first option, Brakes, which limits the first search results.
What I want to see happen is the Tuning Categories be an optional field that shows either a blank entry or "Optional" as the default and to show all search results based on the first 3 fields. I've attached screenshots of my setup, as below is the custom code I use on the form to remove duplicates. Thank you in advance.
<?php
/**
* Modify output of View "Tuner Search"
*
* 1. no initial results until a filter is applied
* 2. avoid duplicates
* 3. promote featured results to the top
*/
add_filter('wpv_filter_query_post_process', 'remove_duplicates_query', 101, 3);
function remove_duplicates_query($query_results, $view_settings, $view_id)
{
$viewid = 1524;
$relationship_slug = 'platform-tuner';
if ( $view_id != $viewid )
return $query_results;
// has a filter been applied?
if ( !isset( $query_results->query['meta_query'] ) && !isset( $query_results->query['tax_query'] ) && !isset( $query_results->query['s'] ) ) {
$query_results->posts = array();
$query_results->post_count = 0;
$query_results->found_posts = 0;
return $query_results;
}
// iterate over results to remove duplicates and to promote featured
$tuners = [];
$featured = [];
$posts = $query_results->posts;
foreach ($posts as $key => $post) {
$tuner_ids = toolset_get_related_posts(
$post->ID,
$relationship_slug,
[
'query_by_role' => 'intermediary',
'role_to_return' => 'child',
'return' => 'post_id'
]
);
$tuner_id = $tuner_ids[0];
if ( in_array( $tuner_id, $tuners ) ) {
// duplicate, discard
unset( $query_results->posts[$key] );
$query_results->post_count --;
$query_results->found_posts --;
} else {
$tuners[] = $tuner_id;
// unique (so far), is it featured?
$is_featured = get_post_meta($tuner_id, 'wpcf-featured', true);
if ( $is_featured ){
$featured[] = $post;
// remove from array of results, to be added back later at the top
unset( $query_results->posts[$key] );
}
}
}
// any featured to add back at top?
if ( $featured ){
$posts = array_merge( $featured, $query_results->posts );
$query_results->posts = $posts;
} else {
$posts = array_values( $query_results->posts );
$query_results->posts = $posts;
}
return $query_results;
}