I'm working on a project and getting same results in different category slug. Please help what I'm doing wrong here.
$act = $_POST['act'];
$args = array(
'posts_per_page' => 100,
'offset' => 0,
'category' => $act,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'product',
'taxnomy' => 'product_cat',
'post_status' => 'publish');
$myposts = get_posts( $args );
global $product;
global $wpdb;
foreach ($myposts as $key => $value) {
$id = $value->ID;
echo '<li class="product type-product status-publish has-post-thumbnail first grid with-hover add-hover open-on-mobile with-border span3 featured shipping-taxable product-type-simple product-cat-accommodation-durban product-cat-accommodation-battlefields product-cat-battlefields product-cat-comfortable-accommodation-durban product-cat-comfortable-accommodation-battlefields product-cat-durban instock">';
echo '<div class="product-wrapper">';
echo '<a class="thumb" href="'.get_permalink( $id ).'">';
$post_thumbnail_id = get_post_thumbnail_id($id);
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
echo '<img src="'.$post_thumbnail_url.'" />';
echo '</a>';
echo '<h3>'.get_the_title( $id ).'</h3>';
echo '</div></li>';
}
<?php
$slug = "category-b";
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $slug
)
),
'post_type' => 'product',
'orderby' => 'title',
);
$the_query = new WP_Query( $args );
foreach ($the_query->posts as $key => $value) {
print_r($value->ID);
}
?>
After careful consideration, I found the root of the problem:
The category needs to be an ID, quoting Wordpress:
Note: The category parameter needs to be the ID of the category, and
not the category name.
Note: The category parameter can be a comma separated list of
categories, as the get_posts() function passes the 'category'
parameter directly into WP_Query as 'cat'.
Note: The category_name parameter needs to be a string, in this case,
the category name.
Taxonomy is not well written, is "taxonomy" and not "taxnomy". Also, you can remove it from there as it's not filtering anything.
Related
after several unsuccessful searches, I ask my question here.
Indeed, I'm trying to display a list of posts grouped by categories:
CAT A
post1
post2
post3
CAT B
post4
post5
post6
post7
...
Here is the code I tried.
I can display the categories, but not the posts
<?php
$terms = get_terms( 'secteur', array(
'orderby' => 'count',
'hide_empty' => 0
) );
foreach( $terms as $term ) {
$args = array(
'post_type' => 'client',
'posts_per_page' => '-1',
'secteur' => $term->slug
);
$query = new WP_Query( $args );
echo'<h3>' . $term->name . '</h3>';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post();
$secteur_dactivite = get_field( 'secteur_dactivite' );
echo '<div class="cat-'.esc_html( $secteur_dactivite->slug ). '"><img src="'.get_field( 'logo' ).'"></div>';
endwhile;
wp_reset_postdata();
}
?>
You need to use tax_query as an WP query attribute, instead of secteur.
Try replacing that with:
$args = array(
'post_type' => 'client',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'secteur',
'field' => 'slug',
'terms' => $term -> slug,
),
),
);
thank you very much for your response.
Unfortunately, this does not change the display. The titles are displayed but not the articles.
<h3>CAT1</h3
<h3>CAT2</h3>
<h3>CAT3</h3>
If it helps, I can display all the items with the following code :
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'client',
'post_status' => 'publish'
));
if($posts)
{
echo '<div class="row all-item">';
foreach($posts as $post)
{
echo '<div"><img src="'.get_field( 'logo' ).'"></div>';
}
echo '</div>';
}
?>
Thank you very much for your answers.
But nothing has worked for me, and I can’t come up with any solutions.
I think the problem lies in the configuration of my taxonomy.
This is my custom post type configuration (client):
https://imgur.com/uDom3PH
my taxonomy configuration (secteur) :
https://imgur.com/WRwsSbR
my custom field (secteur_dactivite) :
https://imgur.com/NKQ4GPn
Thanks again for your help
I want latest one product from all categories display on the page in wordpress , when we add more categories and products in it then it should have to be add(display) on the page with its single latest product.How we can do this? please help me. Thank you
First, you need to get all the product categories with at least a single post using the hide_empty.
Then loop through each category and run a query for each to get the single product.
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
echo '<h4>' . $product_category->name . '</h4>';
$args = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $product_category->slug
)
),
);
$products = new WP_Query( $args );
echo "<ul>";
while ( $products->have_posts() ) {
$products->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php
}
wp_reset_postdata();
echo "</ul>";
}
}
i need to display post from a custom post type but from one specific category, i use the code belowe but show me all post from all categories not just from 7.
<?php
$args = array( 'post_type' => 'tour', 'posts_per_page' => 10, 'cat=7' , 'taxonomy' => 'tourcat');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo "TEST TEST TEST TEST";
echo the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>
This is not a valid argument for WP Query.
cat=7
Please read here about taxonomy queries, they should do what you require:
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Currently your $args array simply holds an element that is cat=7, which is an incorrect way to pass arguments to the WP_Query constructor. Note that the issue is a little more obvious if you format your $args array with whitespace:
$args = array(
'post_type' => 'tour',
'posts_per_page' => 10,
'cat=7' , /* Here is the problem */
'taxonomy' => 'tourcat'
);
I believe your $args array should look like the following:
$args = array(
'post_type' => 'tour',
'posts_per_page' => 10,
'cat' => 7,
'taxonomy' => 'tourcat'
);
This,
$args = array( 'post_type' => 'tour', 'posts_per_page' => 10, 'cat=7' , 'taxonomy' => 'tourcat');
Should instead be like,
$args = array( 'post_type' => 'tour', 'posts_per_page' => 10, 'tax_query' => array( array( 'taxonomy' => 'tourcat','field' => 'ID','terms' => '7' ) ));
Try this :
$postData = new WP_Query(array(
'post_type' => 'tour', // custom post type
'posts_per_page'=>10,
'tax_query' => array(
array(
'taxonomy' => 'tourcat', //custom taxonomy name
'field' => 'id',
'terms' => 7
)
)
));
if($postData->have_posts()):
while ($postData->have_posts()): $postData->the_post();
echo "TEST TEST TEST TEST";
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
endif;
I'm trying to make a loop of a custom type of posts by tag ID.
This is the original code:
<?php $args = array(
'post_type' => 'kana_portfolio',
'meta_key' => 'choose_layout_2',
'meta_value' => 'layout-1',
'posts_per_page' => -1
);
$posts = get_posts($args);
$term_array = array();
$portfolio_term_array = array();
foreach ( $posts as $post ) : setup_postdata( $post );
$terms = wp_get_post_terms( get_the_ID(), 'kana_genre');
foreach($terms as $term){
$term_array[$term->slug] = $term->name;
$portfolio_term_array[] = $term->slug;
}
endforeach;
wp_reset_postdata(); ?>
Since I need to loop only the posts in a certain category (of custom posts type) having id 31 I added "'tag_id' => 31" after "'posts_per_page' => -1"
<?php $args = array(
'post_type' => 'kana_portfolio',
'meta_key' => 'choose_layout_2',
'meta_value' => 'layout-1',
'posts_per_page' => -1,
'tag_id' => 31
);
$posts = get_posts($args);
$term_array = array();
$portfolio_term_array = array();
foreach ( $posts as $post ) : setup_postdata( $post );
$terms = wp_get_post_terms( get_the_ID(), 'kana_genre');
foreach($terms as $term){
$term_array[$term->slug] = $term->name;
$portfolio_term_array[] = $term->slug;
}
endforeach;
wp_reset_postdata(); ?>
[EDIT]
I have another loop below in the page and I tried to insert 'cat'=>31 tax_query array
<?php $number_posts_to_display = get_field('number_of_posts_to_display');
$display_order = get_field('post_display_order');
$portfolio = array(
'post_type' => 'kana_portfolio',
'posts_per_page' => $number_posts_to_display,
'order' => $display_order,
'meta_key' => 'choose_layout_2',
'meta_value' => 'layout-1',
'tax_query' => array(
array(
'taxonomy' => 'kana_genre',
'field' => 'slug',
'terms' => $portfolio_term_array,
),
),
);
$portfolio_loop = new WP_Query($portfolio); ?>
But nothing is showed, how can I do?
if your category (we are talking real categories here, of the taxonomy type category...) is 31, you should use 'cat'=>31, tags are tags, they are not categories.. if this helped, let me know. if you were trying to filter on tags and it was not working, let me know as well and ill take a look in depth.
Have fun!
Instead of:
'tag_id' => 31
Try this:
'tag__in' => 31
How can I list out the top 5 most popular category(or category of most popular products) on my wordpress site home page.
I have used woocommerce plugin for products.
Thanks in advance for any suggestion or solution.
Since none of the answers is a solution to the author's question, here is what I came up with. This is a shortcode snippet that lists popular products by categories. By popular I mean most sold products ( as in total sales).
function bestselling_products_by_categories( $atts ){
global $woocommerce_loop;
extract(shortcode_atts(array(
'cats' => '',
'tax' => 'product_cat',
'per_cat' => '5',
'columns' => '5',
'include_children' => false,
'title' => 'Popular Products',
'link_text' => 'See all',
), $atts));
if(empty($cats)){
$terms = get_terms( 'product_cat', array('hide_empty' => true, 'fields' => 'ids'));
$cats = implode(',', $terms);
}
$cats = explode(',', $cats);
if( empty($cats) )
return '';
ob_start();
foreach($cats as $cat){
// get the product category
$term = get_term( $cat, $tax);
// setup query
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_cat,
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'id',
'terms' => $cat,
'include_children' => $include_children,
)
),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array( 'catalog', 'visible' ),
'compare' => 'IN'
)
)
);
// set woocommerce columns
$woocommerce_loop['columns'] = $columns;
// query database
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php if ( shortcode_exists('title') ) : ?>
<?php echo do_shortcode('[title text="'. $title .'" link="' . get_term_link( $cat, 'product_cat' ) . '" link_text="' . $link_text . '"]'); ?>
<?php else : ?>
<?php echo '<h2>'. $title .'</h2>'; ?>
<?php endif; ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
}
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
} add_shortcode( 'custom_bestselling_product_by_categories', 'bestselling_products_by_categories' );
You can use this by calling it as:
<?php echo do_shortcode('[custom_bestselling_product_by_categories cats="' . $term->term_id . '"]'); ?>
This shortcode has some options:
cats : the category ID or comma-separated IDs to retrieve the products from.
tax : the taxonomy to get the products from, default is product_cat
per_cat : number of products to retrieve
columns : number of columns to display
include_children : if false only direct children of the category will be displayed, if true then children of children will be displayed
title : title to display
link_text : the link text linked to the store
Notice that this snippet assumes you have a shortcode named title and it takes a few other parameters such as link and link_text arguments. You can always change this according to your theme.
Hope it helps.
I recommend you to check this page.
http://docs.woothemes.com/document/woocommerce-shortcodes/
array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc',
'category' => ''
)
[product_category category="appliances"]
array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc'
)
[top_rated_products per_page="12"]
Or you can use this plugin : https://wordpress.org/plugins/sp-woocommerce-best-selling-products-by-category/
Popular might be in many cases like most viewing, top selling. So i listed products by top selling. This way you can get top selling products and by this you can get category listing.
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '10',
'columns' => '4',
'fields' => 'ids',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'meta_query' => WC()->query->get_meta_query()
);
$best_sell_products_query = query_posts($query_args);
return $best_sell_products_query;