Holen Sie sich die Produktliste einer bestimmten Kategorie-ID
-
-
`category` oder`product_category`?`category` or `product_category`?
- 1
- 2014-05-14
- fuxia
-
4 Antworten
- Stimmen
-
- 2014-06-02
Ich vermute,das Hauptproblembesteht darin,dass Sie das Objekt
WP_Query
anstelle vonget_posts()
verwenden sollten. Letzteresgibt standardmäßignur Artikelmit dem Post-Typpost
zurück,nicht Produkte,Beieiner Kategoriemit der ID 26 würde derfolgende Code die Produkte zurückgeben (WooCommerce 3+):
$args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => '12', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', //This is optional, as it defaults to 'term_id' 'terms' => 26, 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ), array( 'taxonomy' => 'product_visibility', 'field' => 'slug', 'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too 'operator' => 'NOT IN' ) ) ); $products = new WP_Query($args); var_dump($products);
$args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => '12', 'meta_query' => array( array( 'key' => '_visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN' ) ), 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', //This is optional, as it defaults to 'term_id' 'terms' => 26, 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ) ) ); $products = new WP_Query($args); var_dump($products);
I suspect the main problem is that you should be using the
WP_Query
object rather thanget_posts()
. The later by default only returns items with a post_type ofpost
not products,So given a category with ID 26, the following code would return it's products (WooCommerce 3+):
$args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => '12', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', //This is optional, as it defaults to 'term_id' 'terms' => 26, 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ), array( 'taxonomy' => 'product_visibility', 'field' => 'slug', 'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too 'operator' => 'NOT IN' ) ) ); $products = new WP_Query($args); var_dump($products);
In earlier versions of WooCommerce the visibility was stored as a meta field, so the code would be:
$args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => '12', 'meta_query' => array( array( 'key' => '_visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN' ) ), 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', //This is optional, as it defaults to 'term_id' 'terms' => 26, 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ) ) ); $products = new WP_Query($args); var_dump($products);
Here we are only returning visible products, 12 per page.
Have a look through http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters for more details about how the category targeting works - it's often more useful to retrieve it by slug than by ID!
-
Lösung hatfunktioniert.Schöne Erklärung.Solution worked. Nice explanation.
- 0
- 2015-07-10
- Kamesh Jungi
-
Ab Woocommerce 3 wird die Sichtbarkeitin Taxonomie anstelle von Metageändert,sodass Sie diemeta_queryin tax_query ändernmüssen.Siehe https://wordpress.stackexchange.com/a/262628/37355.As of Woocommerce 3, visibility is changed to taxonomy instead of meta so you need to change the meta_query to tax_query. See https://wordpress.stackexchange.com/a/262628/37355.
- 1
- 2017-10-18
- jarnoan
-
Ihre Schlussfolgerung zuget_posts ()istfalsch.Sie können "new WP_Query ($ args)" durch "get_posts ($ args)"in Ihrem Codeersetzen undes wirdfunktionieren.Your conclusion about `get_posts()` is wrong. You can replace `new WP_Query($args)` with `get_posts($args)` in your code and it will work.
- 0
- 2018-07-14
- Bjorn
-
-
OP hat speziell darumgebeten,Produktemit einer Kategorie-ID zuerhalten. Dies hatmirjedochgeholfen,sodassichtrotzdemeine positive Bewertung abgeben werde.Seien Sie sichnurbewusst,dass die ursprüngliche Fragenichtbeantwortet wirdOP specifically asked for getting products using a category ID, however, this helped me, so I'll upvote anyhow. Just be aware it doesn't answer the original question
- 1
- 2019-09-24
- dKen
-
-
- 2015-01-19
Ändern Sie die Kategorie (Kategorie-Slug-Name)nach ID oder Name oder Slug
<?php $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 2,'product_cat' => 'category-slug-name', 'orderby' =>'date','order' => 'ASC' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> Within loop we can fetch Product image, title, description, price etc. <?phpendwhile;wp_reset_query(); ?>
change category (category-slug-name) by id or name or slug
<?php $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 2,'product_cat' => 'category-slug-name', 'orderby' =>'date','order' => 'ASC' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> Within loop we can fetch Product image, title, description, price etc. <?phpendwhile;wp_reset_query(); ?>
-
- 2016-11-18
Etwas spät,möchte aber die Dinge klären undeine sauberere Antwortgeben. User @benz001gabeine möglichegültige Antwort,sagte aberetwas Falsches:
------------------------------------------------------------------ Tatsacheist,dass dem OPeinfacheinige Parameterim Arrayget_posts
gibt alle Arten von Post-Typen zurück,standardmäßigposts
Post-Typ,genau wieWP_Query
. Die wirklichen Unterschiede zwischen denbeiden sind wunderbarerklärt HIER .$args
fehlten:Die Definition des Post-Typs,nach demer sucht:
'post_type' => 'product',
-
'tax_query' => array( array( 'taxonomy' => 'product_cat', 'terms' => 26, 'operator' => 'IN', ) )
Auf diese Weise Ihrenächsten Zeilen
$products = new WP_Query($args); var_dump($products);
Zeigt Ihnen diebenötigten Produkte :)
Sebastian Alle anderen zusätzlichen Parameter,die von @benz001 angezeigt werden,sindnatürlichgültig,werdenjedoch vom OPnicht angefordert. Daher habeichbeschlossen,siein dieser Antwort zurückzulassen.A bit late, but would like to clarify things and provide a cleaner answer. User @benz001 gave a possible valid answer, but said something wrong:
get_posts
returns any kind of post-types, defaulting toposts
post-type, just likeWP_Query
. The real differences between the two are wonderfully explained HERE.The fact is, the OP was simply missing some parameters into the
$args
array:The definition of the post-type he is searching for:
'post_type' => 'product',
And the modification of the "taxonomy part" of the search query:
'tax_query' => array( array( 'taxonomy' => 'product_cat', 'terms' => 26, 'operator' => 'IN', ) )
This way your next lines
$products = new WP_Query($args); var_dump($products);
Will show you the needed products :)
All other additional parameters shown by @benz001 are of course valid but not requested by the OP, so I decided to leave them behind in this answer.
Der Code,denich zum Abrufen der Kategorieliste verwende,lautet wiefolgt:
$ Füreine bestimmte Kategorie-ID (z. B. 47) konnteichjedoch keinen Wegfinden,um die relevanten Produkte zuerhalten. Ich habe Folgendes versucht: