WordPress.org

Forums

Custom Query Tag = and Tag != (5 posts)

  1. ywftdg
    Member
    Posted 6 years ago #

    I am trying to do a custom query and it works fine using:
    $query= 'tag=' . $value. '&showposts=1&random=true';

    My only concern is I also want to say where tag = a string, but also that post doesn't contain some other tags.

    For example if I have 2 posts, one with tags = design, poster, happy. Then another post with the tags = design, art, nsfw. I would like to have a way to say, if the post has the tag 'nsfw' skip this item in the query result.

    Any ideas?

  2. ywftdg
    Member
    Posted 6 years ago #

    I have even tryed using a custom query, and the mysql LIKE feature does not work.

    $querystr = "
    				SELECT wposts.*
    				FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    				WHERE wposts.ID = wpostmeta.post_id
    				AND wpostmeta.meta_key = 'tag'
    				AND wpostmeta.meta_value LIKE 'art%'
    				AND wposts.post_status = 'publish'
    				AND wposts.post_type = 'post'
    				ORDER BY wposts.post_date DESC
     			";

    What is the deal with LIKE not working?

  3. ywftdg
    Member
    Posted 6 years ago #

    Bump: nobody out there ever had to query wordpress database using LIKE?

  4. nozero
    Member
    Posted 5 years ago #

    i dont know too much about this stuff, i just started querying the worpress db, but i copied you query to test and i needed to change the table name to "wp_posts" and "wp_postmeta"

  5. Qurl
    Member
    Posted 5 years ago #

    Fairly simple... This should do the trick.

    "SELECT " . $wpdb->prefix . "posts.* FROM " . $wpdb->prefix . "posts JOIN " . $wpdb->prefix . "term_relationships ON " . $wpdb->prefix . "posts.id = " . $wpdb->prefix . "term_relationships.object_id LEFT JOIN " . $wpdb->prefix . "terms ON " . $wpdb->prefix . "term_relationships.term_taxonomy_id = " . $wpdb->prefix . "terms.term_id WHERE " . $wpdb->prefix . "terms.name != 'nsfw' AND " . $wpdb->prefix . "terms.name = 'art' AND " . $wpdb->prefix . "posts.post_status = 'publish' AND " . $wpdb->prefix . "posts.post_type = 'post' GROUP BY " . $wpdb->prefix . "posts.id"

Topic Closed

This topic has been closed to new replies.

About this Topic