Display posts that have two taxonomies in Wordpress wp_query - php

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);

Related

category__not_in in index.php of Woocommerce template not working

I'm new to PHP and WordPress so my question could easily be silly. Although I have found others with similar questions.
On my main page (index.php) I would like to exclude products of a certain category. I found that it should be possible to use category__not_in as a parameter passed to the $args var. but this option doesn't work. When I change other values in the $args var. they are applied. I.e. if I change the post_per_page it is visible immediately. I use the code below and only want to exclude category 28 (could be more in the future). I tried the solutions mentioned in stackoverflow, but no success. A
I was wondering if anyone could point me in the right direction. Thanks
<section id="content">
<div class="container woocommerce">
<?php
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
global $woocommerce_loop, $woocommerce;
$meta_query = $woocommerce->query->get_meta_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'category__not_in' => array( 28 ),
'ignore_sticky_posts' => 1,
'post__not_in' => $slider_arr,
'posts_per_page' => 9,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
$products = new WP_Query( $args );
//query_posts( $args );
if ( $products->have_posts() ) : $x = 0; ?>
<div id="posts_cont">
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product-home' ); ?>
<?php if ($x == 2) { echo '<div class="home_small_box clear"></div>'; $x = -1; } ?>
<?php $x++; endwhile; // end of the loop. ?>
</div><!--//posts_cont-->
<?php //woocommerce_product_loop_end(); ?>
<div class="load_more_cont">
<div align="center"><div class="load_more_text">
<?php
ob_start();
//next_posts_link('<img src="' . get_bloginfo('stylesheet_directory') . '/images/loading-button.png" />');
next_posts_link('LOAD MORE PRODUCT');
$buffer = ob_get_contents();
ob_end_clean();
if(!empty($buffer)) echo $buffer;
?>
</div></div>
</div><!--//load_more_cont-->
<?php
global $wp_query;
//echo '**' . $wp_query->max_num_pages . '**';
$max_pages = $wp_query->max_num_pages;
?>
<div id="max_pages_id" style="display: none;"><?php echo ceil($wp_query->found_posts / 9); //echo $max_pages-1; ?></div>
<?php endif;
//wp_reset_query();
wp_reset_postdata();
?>
<div class="clear"></div>
</div><!--//container-->
category__not_in only work with WordPress categories, but not for WC Product categories
There are some mistakes in your code like:
Product categories is a custom taxonomy, so you need to use a Tax Query instead.
Since Woocommerce 3 woocommerce_get_template_part() is deprecated and replaced by wc_get_template_part()
$meta_query should be used in your query…
$slider_arr is not defined (so I have comment it)
So try this instead:
<section id="content">
<div class="container woocommerce">
<?php
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
global $woocommerce_loop;
$products = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( 16 ),
'operator' => 'NOT IN',
) ),
'meta_query' => WC()->query->get_meta_query(),
// 'post__not_in' => $slider_arr,
'ignore_sticky_posts' => 1,
'posts_per_page' => 9,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
) );
if ( $products->have_posts() ) : $x = 0; ?>
<div id="posts_cont">
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product-home' ); ?>
<?php if ($x == 2) { echo '<div class="home_small_box clear"></div>'; $x = -1; } ?>
<?php $x++; endwhile; // end of the loop. ?>
</div><!--//posts_cont-->
<?php //woocommerce_product_loop_end(); ?>
<div class="load_more_cont">
<div align="center"><div class="load_more_text">
<?php
ob_start();
//next_posts_link('<img src="' . get_bloginfo('stylesheet_directory') . '/images/loading-button.png" />');
next_posts_link('LOAD MORE PRODUCT');
$buffer = ob_get_contents();
ob_end_clean();
if(!empty($buffer)) echo $buffer;
?>
</div></div>
</div><!--//load_more_cont-->
<?php
global $wp_query;
//echo '**' . $wp_query->max_num_pages . '**';
$max_pages = $wp_query->max_num_pages;
?>
<div id="max_pages_id" style="display: none;"><?php echo ceil($wp_query->found_posts / 9); //echo $max_pages-1; ?></div>
<?php endif;
//wp_reset_query();
wp_reset_postdata();
?>
<div class="clear"></div>
</div><!--//container-->
Note: I suppose that 'product-home' is a custom product template so I have test the code with default 'product' instead, to make it work, and the code works with it.

WooCommerce - Fetch specific product category in page template

I would like to fetch WooCommerce product on the basis of product category on my page template.
Lets say I have a category mattress. I want to fetch all the products related to that mattress category.
Here is my code:
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile;
?>
How can I achieve this?
Thanks.
— — ( Update 2 of the August 8th ) — —
Finally a good alternative is to use has_term(); wordpress function to filter all products of some category in an if statement. If you have multiple categories you can embed them in an array using also has_term(); function.
As I have said before below on a comment, I think that your problem is here:
// Include the page content template.
get_template_part( 'content', 'page' );
So I will replace it with some custom code to show you that the condition is working fine:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
// Start the Loop
If($loop->have_posts()){
while ( $loop->have_posts() ) : $loop->the_post();
// Here it is your product category
if ( has_term( 'mattress', 'product_cat', $loop->post ); ) {
// from here I display products thumbnails and name
echo '<div class="woocommerce-product" style="padding:5px; float:left;">';
if (has_post_thumbnail( $product_id )) {
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
} else {
echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />';
}
echo '<div class="product-name">' . $loop->post->post_name . '</div>';
echo '</div>';
}
endwhile;
}
// If needed
wp_reset_query();
wp_reset_postdata();
?>
It also works without condition: has_term( 'mattress', 'product_cat', $loop->post ); replacing $args array by this one:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // you can set here number of post per page
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'mattress '
) )
);
This code is tested and fully functional and it goes on function.php file of your active child theme or theme.
Reference:
Display different custom fields for different categories in WooCommerce
WordPress Function Reference - has_term()
— — ( Update 1 - july 9th ) — —
Or you can also use has_category(); function to filter in an if statement this way:
<?php
global $post;
// Start the Loop
while ( have_posts() ) : the_post();
if ( has_category( 'mattress', $post ); ) {
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
}
endwhile;
?>
You could try this:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // you can set here number of post per page
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'mattress '
) )
);
$loop = new WP_Query($args);
If($loop->have_posts()){
while($loop->have_posts()) {
$loop->the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
}
}
// If needed
wp_reset_query();
wp_reset_postdata();
?>
Below code should work.
<?php
$query = new WP_Query('posts_per_page=5&post_type=product&product_cat=mattress');
If($query->have_posts()){
while($query->have_posts()) {
$query->the_post();
// Your Code
}
}
wp_reset_query();
wp_reset_postdata();
?>
<ul>
<?php
$taxonomy = 'product_cat';
$orderby = 'title';
$show_count = 0;
$pad_counts = 0;
$hierarchical = 1;
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
$category_id = $cat->term_id;?>
<li><?php echo $cat->name;?></li>
<?php }?>
</ul>

Wordpress pagination links reload page

I am trying to build a paginated archive sidebar using WP_Query. As the same sidebar is used on both single.php and news.php, the arguments used for the WP_Query are different.
On news.php, the pagination works perfectly, but on single.php the pagination links are present, and have the correct href values, but simply reload the page when clicked, i.e. anything beyond page 1 is inaccessible.
Update: Just in case this is relevant, single.php makes use of the Share Buttons by AddToAny, and Social (by MailChimp) plugins. I have deactivated both, but this has not fixed the issue with pagination.
Can anyone shed some light on this for me? Code here:
<aside>
<header>
<h1 class="column-title">Archive</h1>
<hr>
</header>
<div>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// remove first four posts if this is the news main page
if ( ! is_single() ) {
// Save first four posts
$first_four = new WP_Query ('post_type=post&orderby=date&order=desc&posts_per_page=4');
if ( $first_four->have_posts() ) : while ( $first_four->have_posts() ) : $first_four->the_post();
$skipIDs[] = $post->ID;
endwhile; endif;
wp_reset_postdata();
// Save all posts
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1
);
$all_posts = new WP_Query($args);
while ( $all_posts->have_posts() ) : $all_posts->the_post();
// Skip first four posts and save rest
if ( in_array($post->ID,$skipIDs) ) { continue; };
$offset_array[] = $post->ID;
endwhile;
wp_reset_postdata();
// Final arguments for WP_Query
$args = array(
'post__in' => $offset_array,
'paged' => $paged,
'posts_per_page' => 5
);
} else {
// Args for single.php
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 5,
'paged' => $paged
);
}
$article_archive = new WP_Query($args);
$max_pages = $article_archive->max_num_pages;
if( $article_archive->have_posts() ) : while( $article_archive->have_posts() ) : $article_archive->the_post();
$has_image = get_the_post_thumbnail($post->ID,'thumbnail'); ?>
<article class="group">
<a class="anchor-overlay" href="<?php the_permalink(); ?>"></a>
<?php if( $has_image ) { echo $has_image; } else { echo "<img src='/wp-content/themes/DCSFC/images/news-calendar/news/no-image.jpg' alt='No image' />"; } ?>
<div>
<h3><?php the_title(); ?></h3>
<time datetime="dd/MM/YYYY"><?php echo get_the_date(); ?></time>
</div>
</article>
<?php endwhile; endif;
wp_reset_postdata();
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $max_pages
) );
?>
</div>

Trying to display posts only from one category in wordpress

I am using the magnet theme and am trying to display posts from a single category on the homepage. I have read through several post but have had no luck using the suggested methods I found.
Here is the snippet of code from the home-page-grid.php file that appears to be adding posts to the homepage
<!-- Start content -->
<div class="grid_8" id="content">
<div class="widget_container content_page">
<div class="post_list_medium_widget">
<div class="post_list_medium_style1">
<?php
global $paged;
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$query = new WP_Query( array ( 'paged' => $paged, 'orderby' => 'date', 'order' => 'DESC' ) );
$row_count=0;
while ( $query->have_posts() ) {
$row_count++;
$query->the_post();
$post_id = get_the_ID();
?>
Any thoughts as to what needs to be done to get this to display a single category and all its sub categories?
Try adding this array item:
'cat' => '14' . 14 is the category id
$query = new WP_Query( array ( 'paged' => $paged, 'cat' => '14', 'orderby' => 'date', 'order' => 'DESC' ) );
<?php
$catPost = get_posts(get_cat_ID("your_category_name")); //change this with your category
foreach ($catPost as $post) : setup_postdata($post); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
<?php endforeach;?>

How to display custom post type by custom taxonomy?

My homepage is displaing my listings( my custom post type) by the order i enter them. I would like my listings that have the custom taxonomy "tag" (Special offer) to be displayed on my first page from my homepage and after that the rest of the listings exactly how they where before. I am new in to wordpress and hope i asked my question right
This is my homepage code
<div id="content">
<?php include (TEMPLATEPATH . '/lib/slider.php'); ?>
<?php
$args = array(
'post_type' => 'listings',
'paged' => $paged,
'showposts' => 8 ,
'oferta' =>"oferta"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="post propbox <?php if (++$counter % 2 == 0) { echo "lastbox"; }?> clearfix" id="post-<?php the_ID(); ?>">
<div class="archimg">
<?php if( has_term( 'featured', 'type', $post->ID ) ) { ?>
<span class="featspan">Featured</span>
<?php } else if ( has_term( 'sold', 'type', $post->ID ) ){ ?>
<span class="soldspan">Sold</span>
<?php } else if ( has_term( 'reduced', 'type', $post->ID ) ){ ?>
<span class="redspan">Reduced</span>
<?php } ?>
<?php
if ( has_post_thumbnail() ) { ?>
<img class="propimg" src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php get_image_url(); ?>&h=180&w=310&zc=1" alt=""/>
<?php } else { ?>
<img class="propimg" src="<?php bloginfo('template_directory'); ?>/images/dummy.jpg" alt="" />
<?php } ?>
</div>
<div class="cover">
<div class="title">
<h2><?php the_title(); ?></h2>
</div>
<div class="propmeta">
<div class="proplist"><span>Price</span> <span class="propval"> <?php $price=get_post_meta($post->ID, 'wtf_price', true); echo $price; ?></span></div>
<div class="proplist"><span>Location</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'location', '', ' ', '' ); ?></span></div>
<div class="proplist"><span>Property type</span> <span class="propval"><?php echo get_the_term_list( $post->ID, 'property', '', ' ', '' ); ?></span></div>
<div class="proplist"><span>Area</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'area', '', ' ', '' ); ?></span></div>
</div>
<div class="entry">
<?php wpe_excerpt('wpe_excerptlength_archive', ''); ?>
<a class="morer" href="<?php the_permalink() ?>">Check this</a>
<div class="clear"></div>
</div>
</div>
</div>
<?php endwhile; ?>
<div class="clear"></div>
<?php getpagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div>
This is my main content content sorry how can i rearenge this so it fits my needs
you can use tax_query like by here
$args = get_posts( array(
'post_type' => 'my_post_type',
'tax_query' => array(
array(
'taxonomy' => 'my_taxonomy',
'field' => 'slug',
'terms' => 'webdesign'
)
)
) );
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
You have to pass the arguments array to you $wp_query->query($args); in which you define the tag name or category name , taxonomy of tag or taxonomy of category
$args = array(
'post_type' => 'your custom post type name',
'paged' => $paged,
'showposts' => 8 ,
'your custom goes here taxonomy' =>"tag name or category name"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
Generally taxonomy name is what you defined in register_taxonomy('here_goes_taxonomy',array('post_type'), array(...))
All the answers above are great, but you don't necessarily need to pass the tax_query array. Once you have created your custom taxonomy the right way, you can do the following.
<?php
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$loop = new WP_Query([
'post_type' => 'your_custom_post_type',
'post_status' => 'publish', // fetch only published posts
'posts_per_page' => get_option('posts_per_page') ?: 10, // number of posts to fetch
'paged' => $paged
'your_custom_taxonomy' => 'your_custom_taxonomy_slug_or_name'
]);
// WordPress pagination doesn't work on custom query
// Let's make it work
$temp_query = $wp_query; // store $wp_query in a temporary variable
$wp_query = null; // set it to null
$wp_query = $loop; // store your custom query to $wp_query
if ( $loop->have_posts() ) { // Let's check if we have some posts
while( $loop->have_posts() ){
$loop->the_post();
// add your markup here
}
}
wp_reset_postdata();
$wp_query = null;
$wp_query = $temp_query; // store back the original $wp_query
<?php $cat_terms = get_terms(
array('mobile_category'),
array(
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'number' => 2 ) );
if( $cat_terms ) : ?>
<?php foreach( $cat_terms as $term ) :
$args = array(
'post_type'=> 'mobile',
'posts_per_page'=> 2,
'post_status'=> 'publish',
'tax_query'=> array(
array(
'taxonomy' => 'mobile_category',
'field' => 'slug',
'terms' => $term->slug,), ),
'ignore_sticky_posts' => true
);
$_posts = new WP_Query( $args );
if( $_posts->have_posts() ) :
while( $_posts->have_posts() ) : $_posts->the_post(); ?>
<span class="color2"><?php echo $term->name ; ?></span>
<?php endwhile;
endif;
wp_reset_postdata();
endforeach; ?>
<?php endif; ?>

Categories