WordPress tax_query基于性别

时间:2013-08-09 17:52:13

标签: wordpress

我有一个令我困惑的WordPress tax_query。

我有与产品相关的帖子。每种产品都属于与性别相关的类别。

所以有些帖子是'男装',有些是'女装'。但有些产品同时属于'男士'和'女士的'。例如一个背包。然后一些产品(如电器)不属于“男装”或“女装”。

那么如何使用WP_Query查询呢?你可以在下面看到我的尝试,但是当帖子同时包含'男装'和'女装'时它不起作用。任何想法?

// Build Gender Tax-Query, based on known user gender
if($user_gender == 'male') {
    // User is a male
    $gender_query = array(
      'taxonomy'    => 'category',
      'field'   => 'slug',
      'terms'   => 'womens',
      'operator'    => 'NOT IN'
    );

} elseif($user_gender == 'female') {
    // User is a female
    $gender_query = array(
      'taxonomy'    => 'category',
      'field'   => 'slug',
      'terms'   => 'mens',
      'operator'    => 'NOT IN'
    );          
} else {
    // User is unknown gender
    $gender_query = array();
}

// Get posts
$args = array(
    'post_type'         => 'post',
    'post_status'       => 'publish',
    'posts_per_page'    => 2,
    'orderby'           => 'rand',
    'tax_query'         => array(
        'relation'      => 'AND',
        $gender_query
    ),
);
$posts = get_posts($args);

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我建议构建一个自定义SQL查询以传递给$wpdb,因为复杂的分类查询比SQL语句更难实现。

注意:您始终可以output the most recent SQL WordPress executed。这是调试这类问题的一种非常好的方法。

回到你的问题,这就是最终的代码片段:

$gender = 'unknown';

$taxonomy = array('male' => 'mens', 'female' => 'womens');

$where_clause = '';
if ( array_key_exists($gender, $taxonomy) ) {
    $where_clause = sprintf("WHERE %s = '%s'", "$wpdb->terms.slug", $taxonomy[$gender]);
}

$query = "SELECT $wpdb->posts.* FROM $wpdb->posts
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
$where_clause";

$posts = $wpdb->get_results($query);

<强>更新

如果您更喜欢使用tax_query:

$gender = 'unknown';

// Build Gender Tax-Query
if($gender == 'male') {
    $gender_query = array(
        'relation'      => 'AND',
        array(
            'taxonomy'    => 'category',
            'field'   => 'slug',
            'terms'   => 'mens',
            'operator'    => 'IN'
        )
    );

} elseif($gender == 'female') {
    $gender_query = array(
        'relation'      => 'AND',
        array(
            'taxonomy'    => 'category',
            'field'   => 'slug',
            'terms'   => 'womens',
            'operator'    => 'IN'
        )
    );
} else {
    $gender_query = array(
        'relation'      => 'AND',
        array(
            'taxonomy'    => 'category',
            'field'   => 'slug',
            'terms'   => array('mens', 'womens'),
            'operator'    => 'IN'
        )
    );
}

// Get posts
$args = array(
    'post_type'         => 'post',
    'post_status'       => 'publish',
    'posts_per_page'    => 2,
    'orderby'           => 'rand',
    'tax_query'         => $gender_query 
);
$posts = get_posts($args);

这两个答案的关键是你不应该基于性别排除项目,而是包括(假设性别中性项目在女性男装)。