Using an object's property in array - php

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

Related

Wordpress filter by taxonomy ACF field

I have a problem searching for posts for which I have created a special field in taxonomy.
$args = array(
'posts_per_page' => '20',
'paged' => $paged,
'post_type' => 'cars',
'order' => 'DESC',
);
Taxonomy name: localization
With my cars post type I have a taxonomy relation, where there is a field I created called "city".
How can I filter posts from "cars" post type by this custom field in taxonomy in wp_query?
I tried to write such tax_query, but I keep doing something wrong. Can you give me an example where someone filters it in a similar way by custom field?
Your query would look something like:
$args = array(
'post_type' => 'cars',
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'localization',
'field' => 'slug',
'terms' => array( 'tax1', 'tax2' )
)
)
);
$query = new WP_Query( $args );

WordPress: Get and display posts which have the same term

I have a custom taxonomy called type. Type has the following options:
Blog
Case study
Webinar
When on a post, I want to showcase posts which have the same type. I.e. if you're on a case study, I want it to display other case studies.
The maximum related posts I want to show is two, but if there is only one post that has that tag, then I only want it to display one.
Not sure where my query is falling apart:
global $post;
$blog_type = get_terms('type');
$args = array(
'post_type' => 'resources',
'category__in' => wp_get_post_categories($post->ID ),
'posts_per_page' => 2,
'post__not_in' => array($post->ID ),
'terms' => $blog_type,
);
$relatedPosts = new WP_Query( $args );
You need to use tax_query here:
$term_list = wp_get_post_terms( $post->ID, 'type', array( 'fields' => 'ids' ) );
$args = array(
'post_type' => 'resources',
'posts_per_page' => 2,
'post__not_in' => array( $post->ID ),
'tax_query' => array(
array(
'taxonomy' => 'type',
'field' => 'term_id',
'terms' => $term_list
)
)
);
$relatedPosts = new WP_Query( $args );

Order by custom Woocommerce product sorting in a WP_Query

I have created a shortcode to display products by category with the query below:
$atts = shortcode_atts( array (
'type' => 'product',
'posts' => -1,
'category' => '',
), $atts, 'list_products' );
$query = new WP_Query( array(
'post_type' => $atts['type'],
'posts_per_page' => $atts['posts'],
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
) ),
) );
This is all fine, however I am trying to use the order by attribute when I sort the products using the custom sort shown here
I have added: 'orderby' => 'modified', and tried some others from here.
Two questions:
Which attribute do I need to use to sort when using custom sorting
And
What do I need to do to make my orderby attribute work when added to the above query? Because it which ever attribute value I use it always sorts by published descending.
When you use custom sorting for Woocommerce products, it set some values for each product in wp_posts database table under menu_order column.
Then you just need to add 'orderby' => 'menu_order', in your WP_Query, so in your code:
$atts = shortcode_atts( array (
'type' => 'product',
'posts' => -1,
'category' => '',
), $atts, 'list_products' );
$query = new WP_Query( array(
'post_type' => $atts['type'],
'posts_per_page' => $atts['posts'],
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
) ),
'orderby' => 'menu_order',
// 'order' => 'DESC',
) );
It will work (by default the order sorting argument is set to ASC normally).

Excluding Specific Terms from WP_Query

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

Wordpress: Dynamically display related posts based on custom taxonomy term?

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

Categories