Advanced custom fields filtering with taxonomies - wordpress

I'm working with AdvancedCustomFields and Taxonomies, and I don't find, in the documentation, something that allows me to filter the Taxonomies by the values stored on the wp_options table.
I found that here.
And, it could by something like this, but with Taxonomies:
I have a taxonomy called "Person", and I have many fields. For example, I would like to filter by sex and country.
Is there any function that allows me to do this? Or should I work with WP-Query?
Thanks in advance

Once you have registered the taxonomy with your post you may try with tax_query Query inside the your query_post
query_posts( array(
'post_type' => 'your post type',
'paged' => $paged,
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'person', //or tag or custom taxonomy
'field' => 'id'
)
)
) );

Related

Get all posts by multiple categories

I want to get all posts by specifying multiple categories from drop-down list. In
pre_get_posts i want to modify the query to search by selected categories(more than one).
$taxquery = ($taxquery, array(
'taxonomy' => 'cat',
'field' => 'id',
'terms' => array('1','2'),
'operator'=> 'IN'
));
$query->set( 'tax_query', $taxquery );
But when I execute this code, non of the posts are shown(I am sure that there is posts in at least one of that two categories)
Maybe there is better way for searching by more than one category by using OR when search by the first,second... categories.
Any directions would be great!
First you should use term_id, instead id, and then you should add additional array to your tax_query, because you nay have more than one. So your code will look like this:
$category_ids = array(1,2);
$query->set( 'category__in', $category_ids );
After hours of debugging I finally found the solution...
There was several mistakes as: Ids needs to be Integers in terms array, taxonomy needs to be 'category' not 'cat', in my case the relation needs to be OR as I want to search by multiple categories and by other custom field, and at least one category needs to be meet...
Here is example of the code:
$taxquery = array(
'relation' => 'OR',
'post_type' => 'post',
'tax_query' => array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array(1,2,3),
'operator'=> 'IN'
));
$query->set('tax_query', $taxquery);

Wordpress, How to filter by taxonomy from custom post type

I created Custom post type named "Syllabi Archive" and added custom taxonomy Named " Program, Academic Year and Semester " and now I want filter post by Taxonomy, Like shown in the image
enter image description here
I think you need something like this:
query_posts( array(
'post_type' => 'Syllabi Archive', // name without spaces
'posts_per_page' => -1, // show to all posts
'tax_query' => array(
array(
'taxonomy' => 'Program, Academic Year and Semester', //or tag or custom taxonomy without spaces
'field' => 'id',
)
)
) );

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,

Advanced Custom Fields display specific category wordpress

I'm coding a wordpress website, but i've run into some problems.
I have through Advanced Custom Fields make a new post type with a category.
I want to display a specific category in my wordpress loop, but for some reason it will not work.
<?php
$args=array(
'post_type' => 'medlem',
'cat' => 4,
);
$medlem = new WP_Query($args);
<?php if ( $medlem->have_posts() ) : while ( $medlem->have_posts() ) : $medlem->the_post(); ?>
It just display all categories, which is not the meaning. Some help?
Are you sure that your Custom Post Type have registered to "category" taxonomy type?
What do you mean by 'I have through Advanced Custom Fields make a new post type with a category.', because ACF is for creating custom fields to existing post types.
I think you could try this code:
$args = array(
'post_type' => 'medlem',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => 4
)
)
);
As 'field' => 'term_id' is default you can skip this line.
For more information go to: http://codex.wordpress.org/Class_Reference/WP_Query

Wordpress Archive Page for a custom taxonomy element

I have a custom post type clientgallery and the custom taxonomy client.
To get all galleries I can type website.com/clientgallery.
But I want to show only galleries of a specific client, like: website.com/clientgallery/miller
So, miller should act like a get parameter.
I already know how to get the galleries by client, but I don't know how to get the parameter part working.
$args = array(
'numberposts' => -1, //limit the number of posts, set 0 if no limit required.
'orderby' => 'post_date', //order by post_date field.
'order' => 'DESC', //order by descending oder.
'post_type' => 'clientgallery', //the post type is custom post type 'News & Events'
'post_status' => 'publish', //post status is 'publish'
'tax_query' => array(
array(
'taxonomy' => 'client', //custom taxonomy slug
'field' => 'slug', //select taxonomy term by slug
'terms' => $_GET['client'] //taxonomy term is called 'home-page'
)
));
Call it taxonomy-client.php :
See all info here on template hierarchy for WordPress : http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
If your taxonomy client is only associated with the clientgallery post type, then website.com/client/miller should suffice to show your list of clientgalleries for this client. I tested it on my site, it works. Or am I getting something wrong?

Resources