简体   繁体   中英

Custom SQL to get posts from array of categories returning id's from excluded categories. What's wrong?

I have this query:

 $querystr = "SELECT  $wpdb->posts.* FROM  $wpdb->posts 
INNER JOIN  $wpdb->term_relationships 
      ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) 
INNER JOIN  $wpdb->postmeta 
      ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id ) 
INNER JOIN $wpdb->postmeta AS mt1 
      ON ( $wpdb->posts.ID = mt1.post_id ) 
WHERE 1=1 AND $wpdb->term_relationships.term_taxonomy_id 
IN (".implode(',',array_unique($categories)).") 
AND $wpdb->posts.post_type = 'post' 
AND ($wpdb->posts.post_status = 'publish') 
AND ( ( $wpdb->postmeta.meta_key = 'joke_type' 
AND CAST($wpdb->postmeta.meta_value AS CHAR) = '".$type."' ) 
AND ( mt1.meta_key = 'joke_rating' AND CAST(mt1.meta_value AS SIGNED) >= '3' ) ) 
GROUP BY $wpdb->posts.ID ORDER BY $wpdb->posts.ID DESC";

Which computes properly when I print the string, but when executed it throws ID's from categories that are not included in this select, for example category 58 which I've checked 10000 times and I'm sure it's not in the SQL string. I ran this in the database as well, same result..I can see posts that are not in those categories.

What am I doing wrong here?

Here's the query string:

SELECT wp_njkf_posts.* FROM wp_njkf_posts 
INNER JOIN wp_njkf_term_relationships 
      ON (wp_njkf_posts.ID = wp_njkf_term_relationships.object_id) 
INNER JOIN wp_njkf_postmeta 
      ON ( wp_njkf_posts.ID = wp_njkf_postmeta.post_id ) 
INNER JOIN wp_njkf_postmeta AS mt1 
      ON ( wp_njkf_posts.ID = mt1.post_id ) 
WHERE 1=1 
AND wp_njkf_term_relationships.term_taxonomy_id 
         IN (
139,133,45,135,38,40,69,85,112,123,53,55,81,36,121,95,83,9,79,35,104,
60,11,101,63,30,122,68,12,98,32,125,124,119,88,129,61,113,108,100,99,   

126,140,131,13,14,105,15,16,49,43,41,74,114,62,94,65,141,17,115,51,18,
19,72,109,93,87,56,20,111,67,34,66,82,21,22,84,75,71,120,46,118,
31,86,70,23,90,110,78,92,24,91,89,73,47,64,29,107,97,130,
1,44,59,76,77,80,54,25) 
    AND wp_njkf_posts.post_type = 'post' 
    AND (wp_njkf_posts.post_status = 'publish') 
    AND ( ( wp_njkf_postmeta.meta_key = 'joke_type' 
    AND CAST(wp_njkf_postmeta.meta_value AS CHAR) = 'joke' ) 
    AND ( mt1.meta_key = 'joke_rating' 
    AND CAST(mt1.meta_value AS SIGNED) >= '3' ) ) 
GROUP BY wp_njkf_posts.ID ORDER BY wp_njkf_posts.ID DESC

LATER EDIT, tried this as well. This returns 0 rows:

    SELECT p . * 
    FROM wp_njkf_posts p, wp_njkf_term_relationships r, wp_njkf_postmeta pm
    WHERE p.ID = r.object_id
    AND p.ID = pm.post_id
    AND r.term_taxonomy_id
    IN ( 139, 133, 45, 135, 38, 40, 69, 85, 112, 123, 53, 55, 81, 36, 121, 95, 83, 9, 79, 35, 
104, 60, 11, 101, 63, 30, 122, 68, 12, 98, 32, 125, 124, 119, 88, 129, 61, 113, 108, 100, 99, 
126, 140, 131, 13, 14, 105, 15, 16, 49, 43, 41, 74, 114, 62, 94, 65, 141, 17, 115, 51, 18, 19, 
72, 109, 93, 87, 56, 20, 111, 67, 34, 66, 82, 21, 22, 84, 75, 71, 120, 46, 118, 31, 86, 70, 23, 
90, 110, 78, 92, 24, 91, 89, 73, 47, 64, 29, 107, 97, 130, 1, 44, 59, 76, 77, 80, 54, 25 ) 
    AND p.post_type =  'post'
    AND p.post_status =  'publish'
    AND (
    pm.meta_key =  'joke_type'
    AND CAST( pm.meta_value AS CHAR ) =  'joke'
    AND pm.meta_key =  'joke_rating'
    AND CAST( pm.meta_value AS SIGNED ) >=  '3'
    )
    GROUP BY p.ID
    ORDER BY p.ID DESC 
    LIMIT 0 , 30

The problem was in my database. It's solved. The query works fine as the first variant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM