WordPress.org

Ready to get started?Download WordPress

Forums

meta key Not Equal to (9 posts)

  1. JoE
    Member
    Posted 4 years ago #

    Trying to load my query and I want it to NOT pull a certain meta value. Trying to also pull dates from the previous week, not sure if that is possible. Here's what I've got, any direction would be appreciated.

    $weekly_article_args=array(
       'category__in' => $cat,
       'showposts' => 3,
       /* meta_key NOT 'pixel_video', meta_value NOT 'yes' */
       /* show posts from THIS week only */
       'caller_get_posts' => $do_not_show_stickies
       );
  2. esmi
    Forum Moderator
    Posted 4 years ago #

  3. JoE
    Member
    Posted 4 years ago #

    Do you know if it needs to be query_posts() for it to work? I'm running the following using WP_Query().

    This one works:
    $weekly_article = new WP_Query("category_name=pixelosophy&showposts=$showposts&caller_get_posts=1");

    But as soon as I try and tell it to not pull ones with a certain meta value, it fails:
    $weekly_article = new WP_Query("category_name=pixelosophy&showposts=$showposts&caller_get_posts=1&meta_key=pixel_video&meta_compare=!=&meta_value=yes");

  4. vtxyzzy
    Member
    Posted 4 years ago #

    SQL does not use '!=' for not equal, it uses '<>'.

  5. MichaelH
    Member
    Posted 4 years ago #

    Maybe this will help you - this is the SQL that generates:

    SELECT SQL_CALC_FOUND_ROWS  wp_posts.*
    FROM wp_posts
    INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
    JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
    WHERE 1=1
    AND wp_term_taxonomy.taxonomy = 'category'
    AND wp_term_taxonomy.term_id IN ('0')
    AND wp_posts.post_type = 'post'
    AND (wp_posts.post_status = 'publish'
    OR wp_posts.post_status = 'private')
    AND wp_postmeta.meta_key = 'pixel_video'
    AND wp_postmeta.meta_value != 'yes'
    GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 5

    Find out the ID for your philosophy category and use cat=x (where x is that ID) in place of category_name=pixelosophy [edit]

  6. MichaelH
    Member
    Posted 4 years ago #

    vtxyzzy - if that is true, that would make line 2139 (at 2.9.1) in query.php wrong
    if ( ! isset($q['meta_compare']) || empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )

  7. vtxyzzy
    Member
    Posted 4 years ago #

    Sorry about that, I typed it wrong when I was testing.

  8. JoE
    Member
    Posted 4 years ago #

    I was using the category number in it's place before. Found out I could just put the name in though since category_name=pixelosophy works too. It's just the meta_key=pixel_video&meta_compare=!=&meta_value=yes part that's not working. Which, I copied directly from: http://codex.wordpress.org/Template_Tags/query_posts#Custom_Field_Parameters

    They've got an example on there for a meta value that does not equal blue, and that's how they have it written. Are you saying it needs to be the category number for it to work?

  9. MichaelH
    Member
    Posted 4 years ago #

    Well assuming that the category pixelosophy is ID 43, what I was saying was that if I used category_name the 'generated' SQL statement used a wp_term_taxonomy.term_id IN ('0') but if I used a category ID it used wp_term_taxonomy.term_id IN ('43')

    Not really testing against any data, just looking at the query.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.