我一直试图找到一种方法来在我的WordPress构建中构建高级自定义搜索 .

我一直在寻找这个帖子以获取信息:https://wordpress.stackexchange.com/questions/165478/creating-a-search-form-for-custom-fields - 但是它似乎没有帮助,我已经陷入困境 .

表格的规格是:

表单需要有3个选择字段,每个字段都有自己的选项集 . 第一个字段需要将搜索结果过滤为自定义帖子类型:postTypeA或postTypeB . 第二个选择字段需要通过在ACF(高级自定义字段)中创建的自定义属性进行过滤,此字段为级别,级别有5个选项:levelA,levelB,levelC,levelD,levelE最后需要按扇区过滤(这有通过类别分类法设置),我们在这里也有5个选项,sectorA等 . 所以希望搜索的结果将得到非常准确的结果 .

到目前为止,我的搜索表单示例如下:

<form method="get" id="advanced-searchform" class="course-finder-form" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
    <input type="hidden" name="search" value="advanced">

    <div class="form-row">
        <label for="who"><?php _e( 'I am a', 'textdomain' ); ?></label>
        <select name="user" id="">
            <option value="learner"><?php _e( 'Learner', 'textdomain' ); ?></option>
            <option value="employer"><?php _e( 'Employer', 'textdomain' ); ?></option>
        </select>
    </div> 

    <div class="form-row">
        <label for="level"><?php _e( 'looking for', 'textdomain' ); ?></label>
        <select name="level" id="">
            <option value="apprenticeship-standards"><?php _e( 'Apprenticeships', 'textdomain' ); ?></option>
            <option value="traineeships-and-internships"><?php _e( 'Traineeships and Internships', 'textdomain' ); ?></option>
            <option value="stand-alone-qualification"><?php _e( 'Stand Alone Qualification', 'textdomain' ); ?></option>
        </select>
    </div>

    <div class="form-row">
        <label for="sector"><?php _e( 'in', 'textdomain' ); ?></label>
        <select name="sector" id="">
            <option value="property"><?php _e( 'Property', 'textdomain' ); ?></option>
            <option value="beauty"><?php _e( 'Beauty', 'textdomain' ); ?></option>
            <option value="business"><?php _e( 'Business', 'textdomain' ); ?></option>
            <option value="digital"><?php _e( 'Digital', 'textdomain' ); ?></option>
            <option value="finance"><?php _e( 'Finance', 'textdomain' ); ?></option>
        </select>
    </div> 

    <input type="submit" class="search-submit colourless-button" value="Find Course" />

</form>

PHP - 添加到functions.php搜索结果页面:

function wpse_load_custom_search_template() {
    if( isset($_REQUEST['search']) == 'advanced' ) {
        require('advanced-search-result.php');
        die();
    }
}
add_action('init','wpse_load_custom_search_template');

PHP - 搜索结果:

<?php get_header(); ?>

<?php
// Get data from URL into variables
$_level = $_GET['level'] != '' ? $_GET['level'] : '';

// Start the Query
$course_args = array(
        'post_type'     =>  'learner_courses', // your CPT
        's'             =>  $_user, // looks into everything with the keyword from your 'user field'
        'meta_query'    =>  array(
                                array(
                                    'key'     => 'level', // assumed your meta_key is 'level'
                                    'value'   => $_level,
                                    'compare' => 'LIKE', // finds levels that matches 'level' from the select field
                                ),
                            )
    );
$courseSearchQuery = new WP_Query( $course_args );

// Open this line to Debug what's query WP has just run
 var_dump($courseSearchQuery->request);

// Show the results
if( $courseSearchQuery->have_posts() ) :
    while( $courseSearchQuery->have_posts() ) : $courseSearchQuery->the_post();
        the_title(); // Assumed your cars' names are stored as a CPT post title
    endwhile;
else :
    _e( 'Sorry, nothing matched your search criteria', 'textdomain' );
endif;
wp_reset_postdata();
?>

<?php get_footer(); ?>

有人请帮助如何通过这些元素过滤搜索,所以首先:发布类型,第二:级别(自定义acf属性),最后:扇区(类别) .

我想我已经把所有东西放在这里解释我的困境,让我知道是否还需要其他东西!

提前谢谢了!