$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Custom Post Type WP_Query with filters and search|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Custom Post Type WP_Query with filters and search

matteradmin10PV0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

This 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.

Post a comment

comment list (0)

  1. No comments so far