Forums

Custom Query - do NOT return posts with defined TAGS (2 posts)

  1. ywftdg
    Member
    Posted 8 months ago #

    I am trying to create a certain NSFW filter on my site. I want to return posts that DO NOT contain certain tags.

    First, I am trying a custom query just testing for posts with tags right now before I even start the final query. But even this first step is returning nothing. Any ideas on custom queries with tags?


    $querystr = "
    SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->term_taxonomy.term_id = 1
    AND $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->postmeta.meta_key = 'tag'
    AND $wpdb->postmeta.meta_value = 'design'
    ORDER BY $wpdb->postmeta.meta_value ASC
    ";

    $pageposts = $wpdb->get_results($querystr, OBJECT);

  2. doodlebee
    Member
    Posted 8 months ago #

    Rather than connect to your own database, etc., why not use WordPress stuff for this? get_the_tags() will output an array of tags for each post within the Loop. You could write a custom function that looks for specific tags, and then just use a new query to skip those posts.

Reply

You must log in to post.

About this Topic