Advanced Custom Fields display specific category wordpress - 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

Related

Display one custom post type at 3 different home page location based on categories

```$specialmenuItems = new WP_Query(array(
'post_type' => 'special_menu',
'posts_per_page' => -1,
));```
this is my custom post type (special menu), in the admin area there are three categories for this post type (dinner, drink and lunch). Main question is how can we display this post type at different locations based on different categories ?
You need to add the category (it is Taxonomy) to the WP_Query:
$specialmenuItems = new WP_Query( array(
'post_type' => 'special_menu',
'tax_query' => array(
array (
// Here is the taxonomy id
'taxonomy' => 'category',
'field' => 'slug',
// Here you put needed category slug
'terms' => 'dinner',
)
),
) );
while ( $specialmenuItems->have_posts() ) :
$specialmenuItems->the_post();
// Show Posts ...
endwhile;
wp_reset_postdata();

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 filtering with taxonomies

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'
)
)
) );

Only show WordPress 'standard' post format on template?

I recently added post formats to my WordPress theme - on the blog page its fine as they are all styled accordingly. However on my home page template I only want to show 'standard' posts formats (no links, galleries, audio, video etc.).
In my theme options I can decide how many posts to display on the front page which is what 'dft_recent_number' is for.
Does anybody know how I can change the code below to exclude all but 'standard' post formats?
<?php
$query = new WP_Query();
$query->query('posts_per_page='.get_option('dft_recent_number'));
//Get the total amount of posts
$post_count = $query->post_count;
while ($query->have_posts()) : $query->the_post();
?>
Any help is much appreciated!
WP_Query does not seem to have a straightforward parameter for post_format.
From my quick research it appears that post formats are related through taxonomies. So, theoretically, using the taxonomy parameters should work.
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-standard',
)
)
);
$query = new WP_Query( $args );
Note: you'll need to update the taxonomy names and slugs for your blog. This should be the names you set in your functions.php file.
// there's no post-format-standard so you should write it like this to exclude all other postpformats
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-quote','post-format-audio','post-format-gallery','post-format-image','post-format-link','post-format-video'),
'operator' => 'NOT IN'
)
I know that's old, but I was facing the same issue and even though I've found a solution I wondered what other have done to "fix" that.
I think a more scalable solution could be something like that:
$post_formats = get_theme_support( 'post-formats' );
$tax_query = false;
if ( $post_formats ) {
$tax_query = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $post_formats[0],
'operator' => 'NOT IN'
)
);
}
// WP_Query arguments
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => $tax_query
);
This would exclude the post formats that have been enabled and also will work in case WP will add more post formats (or add the ability to add more).

Display all posts of a post format.

I want to display all posts of a post format (let's say: aside). In WordPress to display all posts of a certain category, just need to use this URL: mysite.com/category/mycategory. Is there a similar way to display all posts of a post format? Or any other way is also fine for me.
You could use tax_query parameters to get the posts by post_format. For example:
$args = array(
'post_type'=> 'post',
'post_status' => 'publish',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-aside' )
)
)
);
Then you can iterate over the results with get_posts() (or use WP_Query() and a standard a Loop):
$asides = get_posts( $args );
if ( count($asides) ) {
foreach ( $asides as $aside ) {
// do something here...
}
}
// this will get all 'quote' post format
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-quote' )
)
)
);
$query = new WP_query($args);
while($query->have_posts()) : $query->the_post();
// do whatever you want with it
endwhile;
WordPress creates archive pages for each post format automatically.
Use the get_post_format_link() method to generate the link to the WordPress archive for each post format (although this method does not work with the "standard" post format).
See The Loop.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
That code above list all of the post on your wordpress site, so to show all post from a category you need the use the if ( in_category('CategoryNumberHere ') function to only fetch post from a category. You must also know the number of the category in your WordPress installation.
Take a look at the linked page above, it has a full tutorial and layout of how to do such things. The code specific to categories is the second section down.

Resources