Php 使用自定义帖子类型和字段创建高级自定义搜索
我一直在试图找到一种方法,在我的WordPress构建中构建一个高级自定义搜索 我一直在寻找这条线索的信息:-但唉,它似乎没有帮助,我已经陷入困境 表格的规格为: 表单需要有3个选择字段,每个字段都有自己的选项集。 第一个字段需要将搜索结果筛选为自定义帖子类型:postTypeA或postTypeB。 第二个选择字段需要通过在ACF(高级自定义字段)中创建的自定义属性进行筛选。此字段为级别,级别有5个选项:级别a、级别B、级别C、级别D、级别E 最后,它需要按部门进行过滤(这是通过类别分类法设置的),我们这里还有5个选项,sectorA,等等。 因此,我们希望搜索结果能得到极其准确的结果 到目前为止,我的搜索表单示例如下所示:Php 使用自定义帖子类型和字段创建高级自定义搜索,php,wordpress,search,custom-post-type,advanced-custom-fields,Php,Wordpress,Search,Custom Post Type,Advanced Custom Fields,我一直在试图找到一种方法,在我的WordPress构建中构建一个高级自定义搜索 我一直在寻找这条线索的信息:-但唉,它似乎没有帮助,我已经陷入困境 表格的规格为: 表单需要有3个选择字段,每个字段都有自己的选项集。 第一个字段需要将搜索结果筛选为自定义帖子类型:postTypeA或postTypeB。 第二个选择字段需要通过在ACF(高级自定义字段)中创建的自定义属性进行筛选。此字段为级别,级别有5个选项:级别a、级别B、级别C、级别D、级别E 最后,它需要按部门进行过滤(这是通过类别分类法设置
<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-搜索结果:
<?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属性),最后:扇区(类别)
我想我已经把所有需要解释的东西都放在这里了,如果还需要什么,请告诉我
非常感谢 大家好,欢迎来到Stackoverflow。我们在这里帮助您解决特定的编程问题。因为我看不到您为实现目标所做的任何努力(我在这里看不到任何PHP代码,没有SQL查询,只有一个表单),所以我认为您不会得到太多帮助。你应该自己试一下,然后把你迄今为止试过的东西贴出来。如果不行,我们会帮你的。但是没有人会为你写代码。@Twinfriends-很抱歉我没有提供足够的信息,我对Stackoverflow还是很陌生,但我非常感谢这个社区。我现在已经更新了我上面的帖子,以包含我认为相关的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(); ?>