• hi

    Having trouble with a complex mysql query in wordpress which is built depending on a variable in php (see below). Essentially the variable $show decides what to select or to exclude based on a category.

    I can see where the problem is (the NOT IN (9) part) which returns posts which are in category 9 because they are also in other categories – as soon as I list all the categories it’s in the post is not selected. How do I make sure posts which are in 9 are simply not selected? Do I need to use a different type of JOIN or do I need something other than NOT IN which I can have a list of categories for?

    Thanks in advance
    Gar

    $query = "
    		SELECT DISTINCT wp_posts.* FROM wp_posts
    		LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    		LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
    		WHERE wp_posts.post_status = 'publish'
    		AND wp_posts.post_type = 'post'
    		AND wp_term_relationships.term_taxonomy_id ";
    
    //these three select based on category selected
    if ($show == "all") $query .= "NOT IN (9) ";
    if ($show == "project") $query .= "IN (7) ";
    if ($show == "research") $query .= "IN (8) ";
    
    $query .= "ORDER BY post_date DESC";
  • The topic ‘Custom SELECT based on category’ is closed to new replies.