我正在构建一个使用wp_query的ajax搜索,我需要能够使用自定义元和自定义分类名称进行过滤。我已经使自定义元部件工作但无法使用分类法名称进行过滤。到目前为止我得到了什么 -
$useCustomJoins = false;
add_action( 'pre_get_posts', function( $q )
{
if( $title = $q->get( '_meta_or_title' ) )
{
add_filter( 'get_meta_sql', function( $sql ) use ( $title )
{
global $wpdb;
// Only run once:
static $nr = 0;
if( 0 != $nr++ ) return $sql;
// Modified WHERE
$sql['where'] = sprintf(
" AND ( %s OR %s ) ",
$wpdb->prepare( "{$wpdb->posts}.post_title like '%%%s%%'", $title),
mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) )
);
return $sql;
});
}
});
add_filter('posts_join', array('CrownResources', 'filterPostsJoin'), 10, 2);
add_filter('posts_where', array('CrownResources', 'filterPostsWhere'), 10, 2);
add_filter('posts_groupby', array('CrownResources', 'filterPostsGroupby'), 10, 2);
public static function filterPostsJoin($join, $query) {
global $wpdb, $useCustomJoins;
if($useCustomJoins || (is_main_query() && is_search())) {
$join .= "
LEFT JOIN
(
{$wpdb->term_relationships}
INNER JOIN
{$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
INNER JOIN
{$wpdb->terms} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id
)
ON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id ";
}
return $join;
}
public static function filterPostsWhere($where, $query) {
global $wpdb, $useCustomJoins;
if($useCustomJoins || (is_main_query() && is_search())) {
$userWhere = self::getUserPostsWhere();
$queryS = !empty(get_query_var('s')) ? get_query_var('s') : get_query_var('_meta_or_title');
$where .= " OR (
{$wpdb->term_taxonomy}.taxonomy IN('resource_pregnancy_taxonomy', 'resource_counseling_taxonomy', 'resource_classes_taxonomy', 'resource_supplies_taxonomy', 'resource_clothing_taxonomy', 'resource_food_taxonomy', 'resource_housing_taxonomy', 'resource_medical_taxonomy', 'resource_substance_taxonomy', 'adoption_taxonomy', 'employment_taxonomy')
AND
{$wpdb->terms}.name LIKE '%".esc_sql($queryS)."%'
{$userWhere}
)";
}
return $where;
}
protected static function getUserPostsWhere() {
global $wpdb;
$userId = get_current_user_id();
$sql = '';
$status = array("'publish'");
if(0 !== $userId) {
$status[] = "'private'";
$sql .= " AND {$wpdb->posts}.post_author = ".absint($userId);
}
$sql .= " AND {$wpdb->posts}.post_status IN( ".implode(',', $status)." ) ";
return $sql;
}
public static function filterPostsGroupby($groupby, $query) {
global $wpdb, $useCustomJoins;
if($useCustomJoins || (is_main_query() && is_search())) {
$groupby = "{$wpdb->posts}.ID";
}
return $groupby;
}
用法:
global $post, $useCustomJoins;
$original_post = $post;
// query for resource
$queryArgs = array(
'post_type' => 'resource',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_status' => 'publish'
);
//if taxonomies
if (!empty($atts['taxonomies'])){
$queryArgs['tax_query'] = array();
foreach ($atts['taxonomies'] as $tax){
$queryArgs['tax_query'][] = array(
'taxonomy' => $tax['name'],
'field' => 'term_id',
'terms' => $tax['terms'],
'operator' => 'AND',
);
}
}
// filter by meta as well as title
$meta_query = array();
$meta_query[] = array(
'key' => 'resource_description',
'value' => $atts['search'],
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'resource_address',
'value' => $atts['search'],
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'resource_date_time',
'value' => $atts['search'],
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'resource_phone',
'value' => $atts['search'],
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'resource_email',
'value' => $atts['search'],
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'resource_website',
'value' => $atts['search'],
'compare' => 'LIKE'
);
//if there is more than one meta query 'or' then
if(count($meta_query) > 1) {
$meta_query['relation'] = 'OR';
}
//if there's a search
if (!empty($atts['search'])){
// $queryArgs['s'] = $atts['search'];
$queryArgs['_meta_or_title'] = $atts['search']; //not using 's' anymore
$queryArgs['meta_query'] = $meta_query;
$useCustomJoins = true;
}
$resourceQuery = new \WP_Query($queryArgs);
$useCustomJoins = false;
关于如何让他们一起工作的任何想法?我可以让meta过滤器正确搜索,但是一旦我添加了分类法,它仍会过滤但不正确。