简体   繁体   中英

Display posts that have two taxonomies in Wordpress wp_query

I'm looking to display posts if they match two separate taxonomies, which are bridal accessories free and glasgow.

At the moment I can get the wp_query loop code below to show all the results from both taxonomies; however I need the loop to display only the posts that have both the categories selected.

<?php
    /**
     * Create a new WP_Query
     * Set $wp_query object to temp
     * Grab $paged variable so pagination works
     */
    ?>
    <?php
        global $wp_query; $post; $post_id = $post-> ID;
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        rewind_posts();
        $temp = $wp_query;
        $wp_query = NULL;

        $post_type = 'place'; // change this to the post type you want to show
        $show_posts = '30'; // change this to how many posts you want to show
        $category_name = 'bridal-accessories-free,special-offer' // change this to the category name you need

    ?>

    <?php $wp_query = new WP_Query( 'placecategory=' . $category_name . '&post_type=' . $post_type .  '&posts_per_page=' . $show_posts . '&paged=' . $paged ); ?>
        <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
            <p style="font-size: 15px; margin-bottom: 10px;"><span style="font-weight: normal; margin-right: 5px; text-transform: uppercase; color: #a5cc8e;"><?php the_title(); ?></span> <span style="margin-right: 5px; color: #000;"><?php echo get_post_meta($post->ID,'geo_address',true);?></span> <?php $contact = stripslashes(get_post_meta($post->ID,'contact',true)); 
                    if($contact && get_option('ptthemes_contact_on_detailpage') == 'Yes') { ?><?php echo PHONE.": "; ?> <?php echo $contact;?><?php } ?>
        <?php endwhile; ?></p>
    <?php wp_reset_query(); ?>

    </div>

If this can be done, is there anyway to pass the loop query variables into the url. We need this to create a link to the results.

Thanks in advance!

Please try this code

$args = array(
    'post_type' => array('post','reviews'),
    'paged' => $paged,
    'tax_query' => array(
      array(
        'taxonomy' => 'category',
        'terms' => 'android',
        'field' => 'slug'
      ),
      array(
        'taxonomy' => 'review_category',
        'terms' => 'android',
        'field' => 'slug'
      ),
    )
);
query_posts($args);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM