I have a custom post type called "doctor", this is the query I have set for displaying all the posts in the archive template:
$args = array(
'post_type' => 'doctor',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => '12'
);
$loop = new WP_Query( $args );
The Doctor CPT has two taxonomy's setup, Specialty and Locations. I also need to allow the users to filter the list of doctors by Specialty and/or Location and have a search box where they can type a name in and search for that name.
I installed Beautiful Taxonomy Filters, but when I select the Specialty or Location it doesn't change what loads on the page.
I'm looking for a solution that will show all doctors when they first go to the archive page, and the will have the option of narrowing down the list based on their selection.
I have a custom post type called "doctor", this is the query I have set for displaying all the posts in the archive template:
$args = array(
'post_type' => 'doctor',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => '12'
);
$loop = new WP_Query( $args );
The Doctor CPT has two taxonomy's setup, Specialty and Locations. I also need to allow the users to filter the list of doctors by Specialty and/or Location and have a search box where they can type a name in and search for that name.
I installed Beautiful Taxonomy Filters, but when I select the Specialty or Location it doesn't change what loads on the page.
I'm looking for a solution that will show all doctors when they first go to the archive page, and the will have the option of narrowing down the list based on their selection.
Share Improve this question asked Oct 4, 2018 at 16:37 Chris WathenChris Wathen 31 silver badge2 bronze badges 2- If you are looking for filter's I would recommend using facetWP. I've been using this plugin for a while and it has never failed me. The only issue i've ran into were on my end AKA (not properly configuration). Once properly configured, they worked like a charm. facetwp It's either this or you could, you could use isotpe.js for custom filtering – bagpipper Commented Oct 4, 2018 at 16:42
- So what is the problem, exactly? With some research on WP_Query arguments and taxonomy helper functions, this can be done quite easily. Have you tried anything, code-wise? If so, please show your efforts and tell us about the specific problems you are facing. – Hans Commented Oct 4, 2018 at 22:10
1 Answer
Reset to default 0This is what I ended up with:
$specialty_terms = get_terms('specialty');
$specialty_term_ids = wp_list_pluck($specialty_terms, 'slug');
$location_terms = get_terms('locations');
$location_term_ids = wp_list_pluck($location_terms, 'slug');
if( isset($_GET['docname']) ):
$name_search = $_GET['docname'];
else:
$name_search = "";
endif;
if( isset($_GET['specialty']) && $_GET['specialty'] !== ''):
$specialty = $_GET['specialty'];
else:
$specialty = $specialty_term_ids;
endif;
if( isset($_GET['location']) && $_GET['location'] !== ''):
$location = $_GET['location'];
else:
$location = $location_term_ids;
endif;
$args = array(
'post_type' => 'doctor',
'orderby' => 'menu_order',
'order' => 'ASC',
'paged' => get_query_var('paged'),
'posts_per_page' => '8',
'post_title_like' => $name_search,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'specialty',
'field' => 'slug',
'terms' => $specialty
),
array(
'taxonomy' => 'locations',
'field' => 'slug',
'terms' => $location
))
);
global $post;
$loop = new WP_Query( $args );
There might be an easier way of doing this, but I created a text field where they can type a name and I also included two other select fields to select the Location and/or Specialty. I then pull the data in from the URL to modify the query.