我正在构建一个酒店搜索引擎,我首先创建了一个自定义内容类型“hotel”,其中包含;“视图”工具集。我正在使用“Search everything”插件从这个自定义内容中查询自定义字段和自定义分类法,并且我已经从函数中排除了其他内容类型。php。
目前,我有以下自定义字段:
标题DresszipCodecity。。。
我想排除“address”和“title”,因为有些地址或酒店名称包含城市名称,我只需要城市查询的结果。
是否可以排除字段或仅包括城市字段?
非常感谢
我正在构建一个酒店搜索引擎,我首先创建了一个自定义内容类型“hotel”,其中包含;“视图”工具集。我正在使用“Search everything”插件从这个自定义内容中查询自定义字段和自定义分类法,并且我已经从函数中排除了其他内容类型。php。
目前,我有以下自定义字段:
标题DresszipCodecity。。。
我想排除“address”和“title”,因为有些地址或酒店名称包含城市名称,我只需要城市查询的结果。
是否可以排除字段或仅包括城市字段?
非常感谢
如果我理解正确,您需要在查询中包含post meta。这就是meta_query
的参数WP_Query 班级来帮忙。这个meta_query
是数组的数组。
法典中的示例:
$args = array(
\'post_type\' => \'my_custom_post_type\',
\'meta_key\' => \'age\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
\'meta_query\' => array(
array(
\'key\' => \'age\',
\'value\' => array(3, 4),
\'compare\' => \'IN\',
)
)
);
$query = new WP_Query($args);
如果您想直接在$wpdb中查询,可以在搜索中这样做。php:
global $wp_query;
global $wpdb;
global $post;
$search = sanitize_text_field( $_GET[\'s\']);
$querystr = "
SELECT wposts.ID, wpostmeta. *
FROM $wpdb->posts AS wposts
LEFT JOIN $wpdb->postmeta wpostmeta ON (wposts.ID = wpostmeta.post_id)yy
WHERE wposts.post_status = \'publish\' // all published
AND wposts.ID NOT IN (\'186\',\'184\' ,\'108\', \'124\', \'78\', \'80\', \'289\') // exclude all these IDs
AND wpostmeta.meta_key= \'_txt_en\' // Here the customField
AND wpostmeta.meta_value LIKE \'%".$search."%\'
GROUP BY wposts.ID
";
$postids = $wpdb->get_results($querystr,OBJECT);
if ($postids){
foreach ($postids as $post){
setup_postdata($post);
echo apply_filters(\'the_content\', get_post_meta($post->ID, \'_txt_en\', true));
}
}else{
echo "<p>Sorry, no matches.";
}
}
我正在索引中使用下面的查询。php文件作为主查询,每当我尝试转到第二页时,我都会遇到404错误,我尝试使用posts\\u per\\u页面,但没有效果。<?php $args = array( \'post_type\' => array( \'post\', \'videos\', \'music\' ), \'tax_query\' => array( array( \'taxonomy\' =>