/ 设置 10 秒超时 // 每日统计清 0 runtime_set('todaycomments', 0); runtime_set('todayarticles', 0); runtime_set('todayusers', 0); if ($forumlist) { $fidarr = array(); foreach ($forumlist as $fid => $forum) { $fidarr[] = $forum['fid']; } forum_update($fidarr, array('todayposts' => 0, 'todaythreads' => 0)); } // 清理临时附件 attach_gc(); // 当天24点 $today = strtotime(date('Ymd')) + 86400; runtime_set('cron_2_last_date', $today, TRUE); // 往前推8个小时,尽量保证在前一天 升级过来和采集的数据会很卡 // table_day_cron($time - 8 * 3600); cache_delete('cron_lock_2'); } } } ?>wp query - Custom Post Type WP_Query with filters and search|Concepts Of Algorithm

wp query - Custom Post Type WP_Query with filters and search

admin2025-06-07  10

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749232144a317470.html

最新回复(0)