Compter les publications dans un type de publication personnalisé et une catégorie spécifique
7 réponses
- votes
-
- 2014-03-02
Une solution alternative utilisant WP_Query serait:
$args = array( 'cat' => 4, 'post_type' => 'videos' ); $the_query = new WP_Query( $args ); echo $the_query->found_posts;
An alternative solution using WP_Query would be:
$args = array( 'cat' => 4, 'post_type' => 'videos' ); $the_query = new WP_Query( $args ); echo $the_query->found_posts;
-
-
Vraimentmauvaiseidée.Et si vous avez 15000messages?Vous lesmettez TOUSen mémoire?Voilà legenre d'idéesfausses quipourraient détruire un site deproduction.Really bad idea. What if you have 15000 posts? You put them ALL in memory? These are the kind of misconceptions that could wreck a production site.
- 10
- 2016-03-04
- Cranio
-
Comme lementionne @Cranio ci-dessus:terribleidée d'obtenirtous lesmessagesjustepour les compter.As @Cranio mentions above: terrible idea to get all the posts just to count them.
- 0
- 2017-07-28
- dhuyvetter
-
-
- 2015-11-10
Cela devrait égalementfonctionner:
$category = get_term('work', 'category'); $posts_in_category = $category->count;
This also should work:
$category = get_term('work', 'category'); $posts_in_category = $category->count;
-
Cela échouera si lataxonomie aest attribuée àplus d'untype depublicationet que vous avezjustebesoin dunombre depublications d'untype depublicationThis will fail if the a taxonomy is assined to more than one post type and you just need the post count of one post type
- 5
- 2015-11-10
- Pieter Goosen
-
- 2015-08-26
Pour unetaxonomiepersonnalisée spécifique,essayez:
$the_query = new WP_Query( array( 'post_type' => 'CUSTOM_POST_TYPE', 'tax_query' => array( array( 'taxonomy' => 'CUSTOM_TAXONOMY', 'field' => 'id', 'terms' => TERM_ID ) ) ) ); $count = $the_query->found_posts;
Documentation sur https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
For a specific custom taxonomy try:
$the_query = new WP_Query( array( 'post_type' => 'CUSTOM_POST_TYPE', 'tax_query' => array( array( 'taxonomy' => 'CUSTOM_TAXONOMY', 'field' => 'id', 'terms' => TERM_ID ) ) ) ); $count = $the_query->found_posts;
Documentation at https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
-
- 2020-08-20
J'aitrouvé celaen examinantmoi-même une chose similaire,alors voicima solution au cas où cela serait utilepour quelqu'un d'autre ... Remarque: la réponse d'Harmonicfonctionne,selon le scénario,bien qu'ilpuisse êtreplusfacile de lefaire à laplace:
$count = get_category($category->term_id)->category_count;
Où
$category
est votre objet detaxonomie.Remarqueimportanteici étant que cela suppose qu'aucun autrepost_typen'utilise lamêmetaxonomie. Détails:
get_category()
esten fait unefonction wrapper deget_term()
.Dans ce cas,get_term () a unparamètrename__like queget_category ()n'apas.Il y aprobablement aussi d'autrespetites différences.
Voir: get_term () get_category
Found this while looking into a similar thing myself so here's my solution in case it's useful for anyone else... Note: Harmonic's answer works, depending on scenario though it may be easier to do this instead:
$count = get_category($category->term_id)->category_count;
Where
$category
is your taxonomy object.Important note here being that this assumes no other post_type uses the same taxonomy. Details:
get_category()
is actually a wrapper function ofget_term()
.In this case, get_term() has a name__like parameter that get_category() doesn't. There are probably other little differences too.
See: get_term() get_category
-
- 2014-07-24
Fondamentalement,si vous lefaites avec la solution que vous aveztrouvée,vousgaspillerezbeaucoup de ressources debase de données lorsque vous aurezbeaucoup demessages à récupérer.
$query = new WP_Query(); echo $query->found_posts();
Cependant,WP_Query->found_posts récupère simplement 'posts_per_page'et effectue COUNT (*)travailmysqlpour vous. Je vous recommande donc d'utiliser ce dernier.
Basically if you do it with your found solution, you will waste quite much DB resources when you have lots of posts to fetch.
$query = new WP_Query(); echo $query->found_posts();
However WP_Query->found_posts just fetch 'posts_per_page' and do COUNT(*) mysql job for you. So I recommend you to use the latter one.
-
- 2019-04-04
Je sais qu'il s'agit d'un ancienfil de discussion,maisil apparaîten premier dans Google,voici donc la VRAIE solutionpour savoir commentprocéder.
$term = get_term( $termId, $taxonomy ); $total_in_term = $term->count;
Vous devez donctransmettre l'ID dutermeet lataxonomie.Il s'agit de la solution laplus légèreet de l'avantage detravailler avec destaxonomiespersonnalisées.
I know that this is an old thread, but it shows up first in Google, so here is the REAL solution on how to do this.
$term = get_term( $termId, $taxonomy ); $total_in_term = $term->count;
So you need to pass the ID of the term and the taxonomy. This is the lightest weight solution as well as having the benefit of working with custom taxonomies.
Commentpuis-je ajuster le code ci-dessuspour yparvenir?
Merci!