Escludere una categoria da WP_Query

26 nov 2017, 13:25:18
Visualizzazioni: 53K
Voti: 6

Ho passato gli ultimi giorni a cercare di escludere una categoria da un archivio di easy digital downloads, che sto visualizzando in un widget personalizzato, ma non riesco a nascondere una categoria chiamata 'custom-project' qualunque cosa provi.

Questo è il codice che sto cercando di utilizzare, basato sulle istruzioni da https://codex.wordpress.org/Class_Reference/WP_Query

$argsQuery = array(
    'posts_per_page' => 3,
    'post_type' => 'download',
    'tax_query' => array(
        array(
            'taxonomy' => 'download_category',
            'field' => 'slug',
            'terms' => 'custom-project',
            'include_children' => true,
            'operator' => 'NOT_IN'
        )
    ),
);
$get_latest_downloads = new WP_Query( $argsQuery );                         
$i=1;
while ( $get_latest_downloads->have_posts() ) : $get_latest_downloads->the_post();

//CODICE DEL CORPO DEL WIDGET

$i++;
endwhile;

Ho anche provato a utilizzare 'cat' invece di 'tax_query', ma senza successo poiché la categoria 'custom-project' continua a essere visualizzata all'interno del loop dei post.

$argsQuery = array(
    'posts_per_page' => 3,
    'post_type' => 'download',
    'cat' => '-5',
);
$get_latest_downloads = new WP_Query( $argsQuery );                         
$i=1;
while ( $get_latest_downloads->have_posts() ) : $get_latest_downloads->the_post();

//CODICE DEL CORPO DEL WIDGET

$i++;
endwhile;

Sono sicuro che il nome dello slug e l'ID della categoria siano corretti. Qualsiasi tipo di aiuto è molto apprezzato.

1
Commenti

è sbagliato e deve essere 'operator' => 'NOT IN'. se cambi NOT_IN in NOT IN la funzione funzionerà correttamente.

Khosravi.em Khosravi.em
15 mag 2023 12:51:00
Tutte le risposte alla domanda 2
0
10

Problema 1

Nella tua query tassonomica, dovresti usare NOT IN invece di NOT_IN. Questo impedisce alla tua tax query di funzionare correttamente (assumendo che gli altri campi siano corretti).

Problema 2

Nei tuoi argomenti per WP_Query(), dovresti usare category__not_in invece di cat. Quindi, modifica il tuo codice in:

$argsQuery = array(
    'posts_per_page'   => 3,
    'post_type'        => 'download',
    'category__not_in' => 5 ,
);
26 nov 2017 14:32:49
0

https://codex.wordpress.org/Class_Reference/WP_Query

category__not_in (array) - utilizza l'ID della categoria.

$argsQuery = array(
    'posts_per_page'   => 3,
    'post_type'        => 'download',
    'category__not_in' => array( 5 ), // array, non una stringa
);
17 set 2018 22:41:01