好的,我在搜索自定义字段数据时遇到了这个问题。
本地Wordpress搜索的优点是,它确实很好、快速和强大,但它的局限性在于它只搜索wp_posts
桌子确实如此not frickin\'查询wp_postmeta
桌子
因此,要充分利用本机Wordpress搜索,您可以通过将特定的可搜索自定义字段数据保存到本机来定制要搜索的确切搜索词post_content
作为json编码的数组。
如果您已经在使用本机post_content
字段(inwp_posts
表)适用于一般工作概况GUF(在cpt中job
), 然后只需将此数据迁移到ACF Wysiwyg Editor
字段名为的字段job_post_content
. 此新ACF版本post_content
现在无法搜索。在你的情况下,这可能是一件好事,因为你会有工作简介的废话,你可能无论如何都不想在求职查询中被发现。如果您仍然希望此内容可搜索,那么您仍然可以将此数据包含在我们的可搜索json数组中,如果您愿意的话(我没有显示这一点,但包含起来很简单)。
因此,在本机中存储任何可搜索的数据post_content
, 这就是魔法发生的地方。在代码中跟随我的注释。。。
将此添加到functions.php
// process cpt job post when saving or updating job post
add_action(\'acf/save_post\', \'acf_save_job\', 20);
/**
* action to run modifications when saving or updating the job
* @param int $post_id
* @return void
*/
function acf_save_job($post_id) {
// get our current global post object
global $post;
// check we are on the cpt job else return now
if($post->post_type <> \'job\') return;
// if job post status is publish or draft
if($post->post_status == \'publish\' || $post->post_status == \'draft\') {
// create job object from current post object
$job = $post;
// set an empty searchable data array
$searchable_job_data = [];
// we dont need to add the native post title for cpt job as this is already searchable
// get our secondary title
$secondary_title = get_field(\'secondary_title\',$post_id);
// if secondary title exists
if($secondary_title) {
// add secondary title to searchable data array
$searchable_job_data[\'stitle\'] = $secondary_title;
}
// get our job links post object field, acf must return this field as post object
$job_links = get_field(\'job_links\',$post_id);
// I am assuming you are allowing multiple job links to be selected in job post admin...
// so we have to loop through each array object and get the job link post title
// if job links var is an array
if(is_array($job_links)) {
// foreach job link item as job object
foreach ($job_links as $key => $job_link_object) {
// if job link object has post title
if($job_link_object->post_title) {
// add job link title to our searchable data array
$searchable_job_data[\'jlinks\'][] = $job_link_object->post_title;
}
}
}
// add more searchable data to $searchable_job_array here if you wish
// convert our searchable data array to json
$searchable_job_json = json_encode($searchable_job_data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
// temp remove the save job action
remove_action(\'acf/save_post\',\'acf_save_job\',20);
// update our native job post content with searchable data
$job->post_content = $searchable_job_json;
// update the current job with new job data
wp_update_post($job);
// re add the save job action
add_action(\'acf/save_post\', \'acf_save_job\', 20);
}
}
因此,当您创建或保存cpt职位时,您会注意到本机
post_content
现在包含如下内容(作为未格式化字符串)。。。
{
"stitle": "Front-end developer",
"jlinks": [
"Web developer",
"Web designer"
]
}
使用cpt或acf,您可以从管理职务帖子页面中直观地隐藏本机帖子内容,但搜索数据仍将存在于数据库中。
After looking at your custom jobs page, would it not be better to turn job_links
, to a taxonomy, rather than a post type. So you can also filter by jobs by selecting job link/service. In my custom jobs archive page example (below) I will also demonstrate this...
这是一个仅用于搜索工作的html搜索表单,可以在您的
header.php
或任何地方。。。
<form action="/jobs" method="get">
<input type="search" name="search" placeholder="Search jobs" />
</form>
以下是一些作业查询功能,可添加到
functions.php
...
/**
* simple method to get request parameter value
* @param mixed $name key name to find
* @param mixed $default default value if no value is found
* @return mixed
*/
function get_url_var($name = false, $default = false)
{
// if name is false
if($name === false) {
return $_REQUEST;
}
// request name var name is set
if(isset($_REQUEST[$name])) {
return $_REQUEST[$name];
}
// return default
return $default;
}
/**
* job custom archive page query
* @return array
*/
function job_query() {
// some vars
$jobs_per_page = 24;
// check url for these filter keys and get value
$job_search = get_url_var(\'search\',false);
$job_link = get_url_var(\'service\',false);
// add more url parameter filters here
// build our jobs query
$job_query = [
\'post_type\' => \'job\',
\'post_status\' => \'publish\',
\'posts_per_page\' => $jobs_per_page,
\'orderby\' => \'ASC\',
\'tax_query\' => [
\'relation\' => \'AND\'
]
];
// filter by search query
if($job_search) {
// extend our jobs query with search
$job_query[\'s\'] = $job_search;
}
// if you convert job_links to taxonomy then...
// filter by job link taxonomy term slug (service)
if($job_link) {
// extend our tax query array
$job_query[\'tax_query\'][] = [
\'taxonomy\' => \'job_link\',
\'field\' => \'slug\',
\'terms\' => [ ],
];
}
return $job_query;
}
现在创建一个名为
Jobs 带页段塞/名称
jobs
. 然后创建一个名为
page-jobs.php
在你的主题根目录中,然后添加下面的php。。。
<?php
// products wp query
$jobs = new WP_Query(job_query());
// get our page header
get_header();
?>
<main>
<?php if($jobs->have_posts()): ?>
<?php while($jobs->have_posts()): $jobs->the_post() ?>
<article id="job-<?=get_the_ID()?>">
<?php the_title(); ?>
<?php the_field(\'secondary_title\'); ?>
<?php the_field(\'jobs_links\'); ?>
</article>
<?php endwhile; ?>
<?php else: ?>
No jobs found matching criteria.
<?php endif; ?>
</main>
<?php
// get our footer
get_footer();
现在使用下面的url格式运行一些搜索测试,或者使用html搜索表单。。。
https://yoursite.com/jobs?search=designer
如果您将
job_links
将类型发布到分类,然后您可以像这样筛选cpt作业。。。(显然,只有在设置ACF时
job_links
要保存/加载术语的分类字段)
https://yoursite.com/jobs?service=web-developer
https://yoursite.com/jobs?service=web-designer
还有一件事,我不确定您的php水平,但如果您使用面向对象的php类,上述内容可能会更多。以上代码用于您的基本function.php
使用者但如果您知道如何使用php类,请告诉我,我可以将代码更新为面向对象的类格式。