J'ai besoin d'obtenir les identifiants pour lesquels la catégorie (taxonomie "catégorie") n'est pas spécifiée. Les postes incluent une autre taxonomie.
Je reçois des messages de code:
$posts = $wpdb->get_results('SELECT ...', , ARRAY_A);
Ma requête sélectionne le type d'enregistrement souhaité
SELECT distinct posts.ID FROM wp_posts AS posts
LEFT JOIN wp_term_relationships AS cat_link ON
cat_link.object_id = posts.ID
LEFT JOIN wp_term_taxonomy AS cat_tax ON
cat_link.term_taxonomy_id = cat_tax.term_taxonomy_id AND cat_tax.taxonomy NOT IN ('category')
WHERE post_type = 'popular_music' AND post_status = 'publish' ORDER BY posts.ID DESC
Mais ce n'est pas considéré:
cat_link.term_taxonomy_id = cat_tax.term_taxonomy_id AND cat_tax.taxonomy NOT IN ('category')
Comment sélectionner l'enregistrement, Uncategorized?
Si je comprends la question, vous recherchez le code SQL pour l’équivalent de
$args = array (
'post_type' => 'popular_music',
'post_status' => 'publish',
'tax_query' => array (
array (
'taxonomy' => 'category',
'operator' => 'NOT EXISTS',
),
),
'posts_per_page' => -1,
'orderby' => 'ID',
'order' => 'DESC',
) ;
$query = new WP_Query ($args) ;
echo $query->request ;
Si tel est le cas, il s'agit du code SQL produit par WP_Query.
SELECT SQL_CALC_FOUND_ROWS $wpdb->posts.ID
FROM
$wpdb->posts
WHERE
1=1 AND
(
NOT EXISTS
(
SELECT 1
FROM
$wpdb->term_relationships INNER JOIN
$wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
WHERE
$wpdb->term_taxonomy.taxonomy = 'category' AND
$wpdb->term_relationships.object_id = $wpdb->posts.ID
)
) AND
$wpdb->posts.post_type = 'popular_music' AND
(($wpdb->posts.post_status = 'publish'))
GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->posts.ID DESC
Le code SQL que vous recherchez ressemble à ceci:
SELECT *
FROM wp_posts AS posts
LEFT JOIN wp_term_relationships
AS cat_link
ON cat_link.object_id = posts.ID
LEFT JOIN wp_term_taxonomy
AS cat_tax
ON cat_tax.term_taxonomy_id = cat_link.term_taxonomy_id
AND cat_tax.taxonomy = 'category'
WHERE post_type = 'post'
AND post_status = 'publish'
AND cat_link.object_id IS NULL