0
votes

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?

1
Could you provide the code which you used to query the taxonomy fields?Robin Vinzenz
@RobinVinzenz I update my question with the code I've tried. I don't know if this was that you asking for.Jabel Márquez
try to add the id of the taxonomy slug behind the meta_value like so: 'documentos_<id>'. if thats works i ll post an answer, I m not sure yetRobin Vinzenz
maybe that ll help you: wordpress.stackexchange.com/questions/73803/… Use for meta_value: "The second parameter needed is a string in the format “$type_$id”.Robin Vinzenz
@RobinVinzenz I've tried with the 'taxonomy_id' string format and doesn't work :(Jabel Márquez

1 Answers

0
votes

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,