Modo elegante per includere solo post pubblicati con get_objects_in_term()?
Il modo più ovvio è iterare attraverso l'array risultante di ID, usare get_post per ognuno e verificare che post_status == 'publish'
. Ma mi chiedo se questo possa causare problemi di memoria dato che get_post
tenterà di default di memorizzare nella cache ogni risultato? A parte un join SQL personalizzato, ci sono argomenti sorprendenti che si possono passare a get_objects_in_term()
o c'è qualche altra funzione di tassonomia che non sto sfruttando e che dovrei usare?

Puoi aggiungere 'post_status' => 'publish'
nella tua query per recuperare solo gli oggetti con stato pubblicato, questo funzionerà per get_posts
, query_posts
o $wp_query
e per includere anche tassonomie personalizzate puoi usare tax_query
nella lista degli argomenti

Grazie per la risposta! Il problema con cui sto combattendo è la cache, che si verificherà sicuramente se usiamo WP_Query in qualsiasi sua forma. get_objects_in_term() è molto più leggero perché restituisce solo gli ID dei post. Lo svantaggio è che non puoi passargli alcun parametro post_status negli argomenti. ahimè.

Non ci sono argomenti che puoi passare. L'unico argomento che viene effettivamente utilizzato è order
. Ecco il codice sorgente della funzione:
<?php
function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
global $wpdb;
if ( ! is_array( $term_ids ) )
$term_ids = array( $term_ids );
if ( ! is_array( $taxonomies ) )
$taxonomies = array( $taxonomies );
foreach ( (array) $taxonomies as $taxonomy ) {
if ( ! taxonomy_exists( $taxonomy ) )
return new WP_Error( 'invalid_taxonomy', __( 'Tassonomia non valida' ) );
}
$defaults = array( 'order' => 'ASC' );
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
$order = ( 'desc' == strtolower( $order ) ) ? 'DESC' : 'ASC';
$term_ids = array_map('intval', $term_ids );
$taxonomies = "'" . implode( "', '", $taxonomies ) . "'";
$term_ids = "'" . implode( "', '", $term_ids ) . "'";
$object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order");
if ( ! $object_ids )
return array();
return $object_ids;
}
Puoi comunque copiare la funzione e aggiungere una clausola aggiuntiva per utilizzare lo stato del post.
<?php
function wpse29749_get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
global $wpdb;
if ( ! is_array( $term_ids ) )
$term_ids = array( $term_ids );
if ( ! is_array( $taxonomies ) )
$taxonomies = array( $taxonomies );
foreach ( (array) $taxonomies as $taxonomy ) {
if ( ! taxonomy_exists( $taxonomy ) )
return new WP_Error( 'invalid_taxonomy', __( 'Tassonomia non valida' ) );
}
$defaults = array( 'post_status' => 'publish', 'order' => 'ASC' );
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
$order = ( 'desc' == strtolower( $order ) ) ? 'DESC' : 'ASC';
$term_ids = array_map('intval', $term_ids );
$taxonomies = "'" . implode( "', '", $taxonomies ) . "'";
$term_ids = "'" . implode( "', '", $term_ids ) . "'";
$object_ids = $wpdb->get_col( $wpdb->prepare(
"SELECT ID from $wpdb->posts WHERE ID IN (
SELECT tr.object_id FROM $wpdb->term_relationships
AS tr INNER JOIN $wpdb->term_taxonomy AS tt
ON tr.term_taxonomy_id = tt.term_taxonomy_id
WHERE tt.taxonomy IN ($taxonomies)
AND tt.term_id IN ($term_ids)
) AND post_status = %s
ORDER BY ID $order", $post_status ) );
if ( ! $object_ids )
return array();
return $object_ids;
}
Non molto diverso. Un elemento aggiuntivo in $defaults
oltre ad alcune modifiche all'SQL.
