I would like to create a foreach loop for taxonomy terms which is for custom post type.
More specifically I want a loop that queries all the products categories, but not the category "special-offers" and not categories subcategories. Bonus would be if, product has no category query them too and order all of them in ASC order (Not like sort products and categories separately. All of them must be sorted at the same time).
So what should I do with my code to make it work as needed?
Current code:
<?php
$args = array(
'post_type' => 'products',
'showposts' => -1,
'post_status' => 'publish',
'parent' => 0,
'hide_empty' => true,
'tax_query' => array(
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => array( 'special-offers', 'other-terms' ),
'operator' => 'NOT IN',
),
);
$terms = get_terms('categories', $args );
foreach ( $terms as $term ) :
echo '<h2>' . $term->name . '</h2>';
endforeach;
?>
Your Tax Query is should be looking within another array.
'tax_query' => array(
array(
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => array( 'special-offers', 'other-terms' ),
'operator' => 'NOT IN',
)
),
Rest of it seems okay.
Check out the WP_Codex on this
Final solution was to add exclude and term id to the taxonomy arguments. Since it is for taxonomy and it uses foreach loop.
$args = array(
'parent' => 0,
'hide_empty' => true,
'exclude' => 13,
);
And answer for how to output custom post type posts with no taxonomy can be found here: http://www.codeforest.net/wordpress-tip-show-posts-no-category-term
Thanks to CBroe and ste for their time.
Just incase someone runs into this in the year 2022 haha
$query = new WP_Query( array( 'category__not_in' => array( 2, 6 ) ) );
Related
I want to use Advanced Custom Fields to determine from which category products will be shown. The ACF field gives the ID of the category and from that I can get the slug. I can't get it to work in the array though, and so it shows all products on the page. Does anyone have suggestions or ideas why it's not working? Thanks in advance!
$term_id = get_field('kies_product_categorie'); //Get category id
$term = get_term_by('id', $term_id, 'product_cat'); //Get terms from given category
$args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $term->slug //Slug of given category
)
)
);
Looking at the documentation here, you appear to be missing the field attribute within the tax_query array.
So your $args should look like:
$args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $term->slug, //Slug of given category
'field' => 'slug'
)
)
);
I need to get the custom posts from a specific parent id and taxonomy.
I'm using this code, and I'm getting ALL the posts that are a child of the ID, but the taxonomy filter isn't affecting
<?php
$posts_array = get_pages(
array(
'post_type' => 'recetas_membresia',
'child_of' => $id_padre,
'tax_query' => array(
array(
'taxonomy' => 'comidas',
'field' => 'slug',
'terms' => $comida->slug
))
));
foreach ($posts_array as $post ) {;?>
<li><?php echo $post->post_title ;?></li>
<?php } ;?>
I have been tried to use compare in the query but still having the same result.
How can I get these specific posts?
From the get_pages function reference I can't see that tax_query would work here. (as it seems to work differently than all the other Post related functions. And I personally never used get_pages)
What you want to use is get_posts (ref).
Instead of child_of you'd use post_parent then to get the children, as listed in the WP_Query::parse_query ref.
$posts_array = get_posts(
array(
'post_type' => 'recetas_membresia',
'post_parent' => $id_padre,
'tax_query' => array(
array(
'taxonomy' => 'comidas',
'field' => 'slug',
'terms' => $comida->slug
)
)
)
);
posts_array = get_pages(
array(
'post_type' => 'recipes',
'child_of' => $id_parent,
'tax_query' => array(
'taxonomy' => 'food',
'field' => 'term_id',
'terms' => $food->term_id
)
));
I have this $args for WP_Query:
$args = array(
'post_type' => 'estructura',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => '12',
),
)
);
Witch returns the posts tagged with the category 12 or tagged with the childs categories ( of 12 )
I want only the ones tagged with category 12,
How can I prevent to return the childs?
Thanks!
I was able to solve this problem using the include_children option
$args = array(
'post_type' => 'estructura',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $tax_id, // 12
'include_children' => false
)
)
);
When you use tax_query, it's getting all of the children as they have the parent's url in their URL (e.g. website.com/category_name/child_1/).
$query = new WP_Query( array( 'cat' => 12 ) );
This should work fine. Source: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
I need to add related posts to my custom post type (using current taxonomy term). I know how to display them, I just need to be able to retrieve the currently selected taxonomy terms.
Here's what I'm doing:
$args = array(
'post_type' => 'mycustomposttype',
'posts_per_page' => 3,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'thetaxonomy',
'terms' => 'theterm',
'field' => 'slug',
)
),
);
I need to dynamically populate "thetaxonomy" and "theterm" based on what's currently selected on the page. Getting the term is easy enough, but I'm struggling to get "thetaxonomy" slug.
Thanks in advance!
FIGURED IT OUT!
In case anyone is looking to accomplish the same thing
$currentTax = get_the_taxonomies($post->ID);
foreach($currentTax as $slug => $tax) {
$currentSlug = $slug;
};
$theTerms = array();
$term_list = wp_get_post_terms($post->ID, $currentSlug, array("fields" => "all"));
foreach($term_list as $term_single) {
array_push($theTerms, $term_single->slug);
}
So the query would look like this...
$args = array(
'post_type' => 'mycustomposttype',
'posts_per_page' => 3,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => $slug,
'terms' => $theTerms,
'field' => 'slug',
)
),
);
Ok so I have everything displaying correctly but if there are terms other than the conditional term 'Non-Marine' within the field 'value' for the custom field "item_tags" it does not display those.
Basically I'm looking for posts with :
1. Custom post_type - ait-dir-item
2. Custom field location - annapolis
3. Custom field item_tags - Non-Marine (this value is within other terms seperated by commas)
I'm also not sure if these values are strings or arrays?
Here is the code I have so far:
<?php
$args = array( 'post_type' => 'ait-dir-item',
'meta_query' => array(
array(
'key' => 'location',
'value' => 'annapolis'
),
array(
'key' => 'item_tags',
'value' => 'non-marine'
)
),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 300 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title('<h3 class="entry-title">', '</h3>');
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>
Thanks for any input!
Cory-
Is there any chance you're mistaking tags for custom fields - "Custom field item_tags which are comma separated"... sounds like a tag to me. Sorry if I'm wrong.
In which case, that part of it would look like this:
'tax_query' => array(
array(
'taxonomy' => 'item_tags',
'field' => 'slug',
'terms' => 'non-marine'
)
)
The whole thing would look like this:
<?php
$args = array( 'post_type' => 'ait-dir-item',
'meta_query' => array(
array(
'key' => 'location',
'value' => 'annapolis'
),
),
'tax_query' => array(
array(
'taxonomy' => 'item_tags',
'field' => 'slug',
'terms' => 'non-marine'
)
)
'orderby' => 'title',
'order' => 'ASC', ... etc
If you wanted comma separated tags, you should really be using custom taxonomies (codex here) anyway!