Conta i post all'interno di un tipo di post personalizzato e di una categoria specifica
7 risposta
- voti
-
- 2014-03-02
Una soluzione alternativa che utilizza WP_Query sarebbe:
$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;
-
-
Idea davveropessima.E se hai 15000post?Li haimessi TUTTIin memoria?Questi sonoi tipi diidee sbagliate chepotrebbero distruggere un sito diproduzione.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
-
Come @Craniomenziona sopra:pessimaidea di riceveretuttii post soloper contarli.As @Cranio mentions above: terrible idea to get all the posts just to count them.
- 0
- 2017-07-28
- dhuyvetter
-
-
- 2015-11-10
Anche questo dovrebbefunzionare:
$category = get_term('work', 'category'); $posts_in_category = $category->count;
This also should work:
$category = get_term('work', 'category'); $posts_in_category = $category->count;
-
Questofallirà se latassonomiaèassegnata apiùdi untipo di articoloe hai solobisogno del conteggio di untipo di articoloThis 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
Per unatassonomiapersonalizzata specifica,prova:
$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;
Documentazione su 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
L'hotrovatomentreesaminavoio stesso una cosa simile,quindiecco lamia soluzionenel caso sia utileper qualcun altro ... Nota: la risposta di Harmonicfunziona,a seconda dello scenario,anche sepotrebbeesserepiù semplicefarlo:
$count = get_category($category->term_id)->category_count;
Dove
$category
èiltuo oggettotassonomia.Notaimportante che qui sipresume chenessun altropost_type utilizzi la stessatassonomia. Dettagli:
get_category()
èin realtà unafunzione wrapper diget_term()
.In questo caso,get_term () ha unparametroname__like cheget_category ()non ha.Probabilmente ci sono anche altrepiccole differenze.
Vedi: 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
Fondamentalmente se lofai con la soluzionetrovata,sprecheraimolte risorse del database quando avraimoltipost da recuperare.
$query = new WP_Query(); echo $query->found_posts();
Tuttavia WP_Query->found_posts recupera 'posts_per_page'e fa COUNT (*) lavoromysqlperte. Quinditi consiglio di utilizzare quest'ultimo.
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
So che questo è un vecchiothread,ma compareperprimoin Google,quindiecco la VERA soluzione su comefarlo.
$term = get_term( $termId, $taxonomy ); $total_in_term = $term->count;
Quindi ènecessariopassare l'ID delterminee latassonomia.Questa è la soluzionepiù leggerae offreil vantaggio di lavorare contassonomiepersonalizzate.
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.
Sto cercando di contare quantipost dal vivo all'interno deltipo dipostpersonalizzato chiamato "video",ma solo quelli della categoria chiamata "lavoro".
Comepossomodificareil codiceprecedenteper ottenere ciò?
Grazie!