0

我有一个让我感到困惑的 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);

谢谢您的帮助!

4

1 回答 1

1

I suggest building a custom SQL query to pass to $wpdb, since complex taxonomy queries are notoriously harder to implement than their SQL equivalents.

Note: You can always output the most recent SQL WordPress executed. This is a very nice way to debug these kind of problems.

Back to your question, this is what the final code snippet would look like:

$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);

Update:

If you prefer to use 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);

The key to both answers is that you should not exclude items based on gender, but rather include (assuming the gender neutral items are properly categorized in womens and mens).

于 2013-08-09T19:53:30.577 回答