WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] OR statement in queries (custom fields OR categories) (7 posts)

  1. Jorim
    Member
    Posted 4 years ago #

    I have a query where I select posts with 4 specific meta values:

    query_posts("&paged=".$page."meta_key=mf_page_type&meta_compare=in&meta_value=news, gig, interview, article&showposts=8&orderby=ASC");

    but I need to add an OR statement to get what I like to see, because I have one category (ID: 21) that over rules those meta values.

    The where in SQL should look like:

    ... and ( (meta_key = 'mf_page_type' and meta_value in ('news', 'gig', 'interview', 'article')) or (cat=21) ) ...

    How can I do this?

  2. MichaelH
    Member
    Posted 4 years ago #

    Possiblities:
    1. use wpdb to get all those posts
    2. use a 'post_where' filter so you can use query_posts
    3. do two queries, keep track of the IDs in the first loop, then exclude those posts in your 2nd query.
    4. do two queries and look at using a plugin such as http://coffee2code.com/wp-plugins/inject-query-posts/ to come up with one query post object

  3. MichaelH
    Member
    Posted 4 years ago #

    #1 above:

    <?php
    $my_posts = $wpdb->get_results("
    SELECT $wpdb->posts . *
    FROM $wpdb->posts
    INNER JOIN $wpdb->term_relationships ON ( $wpdb->posts.ID = $wpdb->term_relationships.object_id )
    INNER JOIN $wpdb->term_taxonomy ON ( $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id )
    JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )
    WHERE (
    $wpdb->posts.post_type = 'post'
    AND $wpdb->posts.post_status = 'publish'
    )
    AND (
    (
    $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->term_taxonomy.term_id
    IN (
    '20'
    )
    )
    OR (
    $wpdb->postmeta.meta_key = 'mf_page_type'
    AND $wpdb->postmeta.meta_value
    IN (
    'news', 'gig', 'interview', 'article'
    )
    )
    )
    GROUP BY $wpdb->posts.ID
    ORDER BY $wpdb->posts.post_date DESC");
    
    if ($my_posts) {
      foreach($my_posts as $post) {
        setup_postdata($post); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      } // foreach($posts
    } // if ($posts
    ?>
  4. Jorim
    Member
    Posted 4 years ago #

    edit: Missed your last post, gonna try that first. Thanks!!!

    Thanks for your post! I can see that you possibilities can do it, but I can't get a clear solution in my head or programmed.

    With query_posts($query_string . "&meta_key=mf_page_type&meta_compare=in&meta_value=news, gig, interview, article"); I show the last 8 posts with meta_key is mf_page_type and the meta_value are news, gig, interview or article.

    I still want to show the last 8 posts (and use paging) with meta_key is mf_page_type and the meta_value are news, gig, interview or article, but, if the category id is 21 I don't care about the meta_value.

    I can use another query to retrieve the post id's with category 21, but then I still have to add those id's with an OR statement in the query_post. With post_where do I see more options, because the original where is extended with my statement and it supports SQL so OR's can be used. But I can't figure out how to set it up in an ordinary loop like:

    <?php if ( have_posts() ) : ?>
    <?php while (have_posts()) : the_post();
  5. MichaelH
    Member
    Posted 4 years ago #

    Before your if (have_posts())

    <?php
    $my_posts = $wpdb->get_col("
    SELECT $wpdb->posts.ID
    FROM $wpdb->posts
    INNER JOIN $wpdb->term_relationships ON ( $wpdb->posts.ID = $wpdb->term_relationships.object_id )
    INNER JOIN $wpdb->term_taxonomy ON ( $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id )
    JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )
    WHERE (
    $wpdb->posts.post_type = 'post'
    AND $wpdb->posts.post_status = 'publish'
    )
    AND (
    (
    $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->term_taxonomy.term_id
    IN (
    '21'
    )
    )
    OR (
    $wpdb->postmeta.meta_key = 'mf_page_type'
    AND $wpdb->postmeta.meta_value
    IN (
    'news', 'gig', 'interview', 'article'
    )
    )
    )
    GROUP BY $wpdb->posts.ID
    ORDER BY $wpdb->posts.post_date DESC");
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
    	'post__in' => $my_posts,
    	'paged' => $paged,
    	'posts_per_page' => 2
      );
    query_posts($args);
  6. Jorim
    Member
    Posted 4 years ago #

    You're my hero! Thanks!

  7. Jorim
    Member
    Posted 4 years ago #

    I'm trying to do the same for the category page, but that messes up my page. I use $catid = get_query_var('cat') to retrieve the category and added

    AND (
    $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->term_taxonomy.term_id IN ('".$catid."')
    )

    to the query. The retrieved posts are correct, but my modified sidebar is behaving as if the page is_home() instead of a category page. Can you tell me whats happening?

    Thanks again!

Topic Closed

This topic has been closed to new replies.

About this Topic