简体   繁体   中英

Filter Custom Posts by Taxonomy Custom Field

I'm using Advanced Custom Field Plugin and I'm trying to filter some Custom Post by a Taxonomy Field , modifying the WP_Query:

$wp_query = new WP_Query(
    array(
        'post_type' => 'recursos', // My custom post
        'posts_per_page' => 12,
        'meta_key' => 'recursos_tipo', // My Taxonomy custom field name
        'meta_value' => 'documentos' // My taxonomy slug; the value for filter
    )
)

If I try filter by a Text Field everything is fine, the WP_Query is modified. But when the field is a Taxonomy field I don't know what parameter should I pass, because is an object. I've tried the taxonomy name and the taxonomy ID but doesn't work.

Is possible filter by a Taxonomy Field ? What parameter for 'meta_value' should I pass? Thanks!

UPDATE - Structure:

Custom Post: 'recursos'.
Custom Taxonomy Slug: 'recursos-tipos' (Group Taxonomy Slug).
Custom Taxonomy: 'documentos' (Taxonomy Slug).
Custom Taxonomy ID: 16.
ACF Taxonomy Field: 'recursos_tipo'.

UPDATE - 'tax_query'

I've tried with this too, and doesn't work. Show me all the posts:

$wp_query = new WP_Query(
    array(
        'post_type' => 'recursos',
        'posts_per_page' => 12,
        'paged' => $paged,
        'tax_query' => array(
            'taxonomy' => 'recursos-tipos',
            'field' => 'slug',
            'terms' => 'documentos'
        )
    )
);

IMPORTANT: I think this is not working because I "assign" the Taxonomies via ACF Taxonomy Field, and it doesn't affect the Tax. My Taxonomies has 0 posts. The tax_query works fine if the Tax has posts. There is a way to affect the post count of Custom Taxonomy via ACF Taxonomy Field?

Did you try WordPress custom query args , just replace "Custom_tax" with your Value: as seen here: WordPress WP_Query

<?php

$jabelquery = new WP_Query(  array( 
'post_type' => 'recursos',  // post,page, revision, custom_post_type
'tax_query' => array(                     //(array) - use taxonomy parameters (available with Version 3.1).
'relation' => 'AND',                      //(string) - Possible values are 'AND' or 'OR' and is the equivalent of ruuning a JOIN for each taxonomy
  array(
    'taxonomy' => 'recursos-tipos',                //(string) - Taxonomy.
    'field' => 'slug',                    //(string) - Select taxonomy term by ('id' or 'slug')
    'terms' => array( 'recursos_tipo' )                 //(string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND'.
     )
) ) 
 );
 // The Loop
 if ( $jabelquery->have_posts() ) :
 while ( $jabelquery->have_posts() ) : $jabelquery->the_post(); ?>


 <?php the_title(); ?>


 <?php endwhile; endif; ?>  

then you can replace custom_tax with ACF field like this:

$jab_tax = get_field('taxonomy_custom_select');
'taxonomy' => $jab_tax,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM