Learning to Display Posts by Taxonomy? - php

Here is the situation, I have a custom tax called Skill. I want to be able to display posts only with skill set as Japanese from English.
I am trying to learn how to use pre_get_posts hook to modify my get_posts query. Here is my example, however I land with the error:
Notice: Undefined variable: postdata
This is what I have tried based on research:
add_filter( 'pre_get_posts', 'wpshout_fundraiser_recent_posts' );
function wpshout_fundraiser_recent_posts( $query ) {
// Fetch only posts tagged with "Japanese from English"
$taxquery = array(
array(
'taxonomy' => 'Japanese from English',
'field' => 'skill',
'terms' => array( 'skill' ),
)
);
$query->set( 'tax_query', $taxquery );
I am sure something is wrong with the above query, which I don't fully understand. Any help and please explain what each field of the array is for if possible.

Try this below code this should work,
add_filter( 'pre_get_posts', 'wpshout_fundraiser_recent_posts' );
function wpshout_fundraiser_recent_posts( $query ) {
$posts_array = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'Your Post Type Name',
'tax_query' => array(
array(
'taxonomy' => 'Japanese from English',
'field' => 'skill',
'terms' => array( 'skill' ),
)
)
)
);
return $posts_array;
}

Here you can check:
$args = array(
'posts_per_page' => 5,
'post_type' => 'Post Type Name',
'tax_query' => array(
array(
'taxonomy' => 'category_taxonomy',
'field' => 'slug',
'terms' => "Category Name"
)));
$query = query_posts( $args );
while (have_posts()) : the_post();
the_content();
endwhile;
view more

$posts_array = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'post_type_name',
'tax_query' => array(
array(
'taxonomy' => 'taxonomy-name',
'field' => 'term_id',
'terms' => $cat->term_id,
)
)
)
);

You can try this below code :
$custom_args=array(
'post_type' => "Your Post Type Name",
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> -1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'tax_query' => array(
array(
'taxonomy' => 'Your taxonomy slug',
'field' => 'slug',
'terms' =>"'Your Category Name"
)
),
'orderby' => 'id',
'order' => 'ASC'
);
$custom_my_query = null;
$custom_my_query = new WP_Query($custom_args);
$custom_my_total_count = count($custom_my_query);
if( $custom_my_query->have_posts() )
{
while ($custom_my_query->have_posts()) : $custom_my_query->the_post();
?>
<?php echo get_the_title($post->ID);?>
<?php
endwhile;
}
wp_reset_query($custom_my_query); // Restore global post data stomped by the_post().
}

Related

Woocommerce Product Variation Tax Query Issue

I'm sure I've either missed something really simple, but is there a reason why this doesn't work?
global $post;
$product_id = $post->ID;
$args = array(
'post_type' => array('product_variation'),
'posts_per_page' => -1,
'post_parent' => 177,
'tax_query' => array(
array(
'taxonomy' => 'pa_billing-period',
'terms' => 'yearly',
'field' => 'slug',
'include_children' => true,
'operator' => 'IN'
)
),
);
$variations = get_posts( $args );
echo "<pre>"; print_r($variations); echo "</pre>";
I'm trying to output a specific variation but I'm getting absolutely nothing. If I remove the tax_query, all 4 variations show.
Any ideas what's missing?
$args = array(
'post_type' => array('product_variation'),
'posts_per_page' => -1,
'post_parent' => 177,
'meta_query' => array(
array(
'key' => 'attribute_pa_billing-period',
'value' => 'yearly'
)
),
);

woocommerce get product order by view

I get product by:
$args = array(
'post_type' => 'product',
'posts_per_page' => 15,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $theCat
)
)
);
$post= new WP_Query( $args );
I want to get products but order by view so I did:
$args = array(
'post_type' => 'product',
'posts_per_page' => 15,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $theCat
)
),
'order' => 'ASC', // add this
'suppress_filters' => false, // add this
'orderby' => 'post_views' // add this
);
$post = new WP_Query( $args );
But still show the same result, any idea?
Not sure if this helps but instead of 'ASC' try 'DESC'?

Woocommerce, get products by category with wp query

I tried many methods, but I can't show products with category name or id
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'cat' => 40,
'posts_per_page' => '12',
);
$loop = new WP_Query( $args );
Try this example,
So given a category with ID 26, the following code would return it's products (WooCommerce 3+):
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => 26,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
'operator' => 'NOT IN'
)
)
);
$products = new WP_Query($args);
var_dump($products);
Try tax_query
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array('40'),
'operator' => 'IN',
)
)
);
$loop = new WP_Query( $args );
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Try this,
<?php
$args = array( 'post_type' => 'product', 'post_status' => 'publish','category' => 34, 'ignore_sticky_posts' => 1, 'posts_per_page' => '12',);
$products = get_posts( $args );
?>
Hope this will helps you. for more info.
I suggest used >> wc_get_product_terms( $vars->ID, 'pa_brand_category' )
please check this screenshot >> http://prntscr.com/hjawu5

Get all products from multiple categories (Woocommerce/Wordpress)

I want to display all products from multiple categories at once.
When i want to display all products from one category my $args array looks like this:
$args = array(
'post_type' => 'product',
'product_cat' => 'backpacks',
'orderby' => '_sku'
);
I remember that I can simply make an array inside my $args:
$args = array(
'post_type' => 'product',
'product_cat' => array(
'backpacks','accessoires',
),
'orderby' => '_sku'
);
But it gives me the following error:
Warning: urlencode() expects parameter 1 to be string, array given in C:\xampp\htdocs\live\wp-includes\formatting.php on line 4312
I know this is a simple thing but i cant figure out why its not working.
Thanks for any help!
Please try below snippet.
$sortcolumn = 'ID';
$prod_categories = array(12, 17); //category IDs
$product_args = array(
'numberposts' => -1,
'post_status' => array('publish', 'pending', 'private', 'draft'),
'post_type' => array('product', 'product_variation'), //skip types
'orderby' => $sortcolumn,
'order' => 'ASC',
);
if (!empty($prod_categories)) {
$product_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $prod_categories,
'operator' => 'IN',
));
}
$products = get_posts($product_args);
Found a simple way to do it
$args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'backpacks'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'accessoires'
)
),
);

WP_Query sort category by term

I'm looking to make the code below sort my custom posttype (Product Type).
So that my active page will only show the posts with the term 'Oil', rather then 'Fuel' and 'Tires'.
$args = array(
'post_type' => 'Products',
'posts_per_page' => -1
);
$my_query = new WP_Query( $args );
if ($my_query->have_posts()) {
while($my_query->have_posts() ) {
echo '<div class="custom-post">';
$my_query->the_post();
echo '<p>' . the_content() . '</p>';
the_post_thumbnail();
echo '</div>';
}
wp_reset_postdata();
}
// Edit, as a reply to the 3 below answers
I assume the foreach loop is a replacement for the WP_Query method, but the tax_query was new to me, and now that I know it exists I looked it up in the codex, however it still wont work. I honostly believe it is something as simple as me naming something in the tax_query wrongly, so I will show my taxonomy code here:
function my_custom_taxonomies() {
register_taxonomy(
'product-type',
'products',
array(
'label' => 'Type of Product',
'rewrite' => array( 'slug' => 'race-products' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'my_custom_taxonomies' );
And the change to the $args:
$args = array(
'post_type' => 'Products',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product-type',
'field' => 'slug',
'terms' => 'oil'
),
),
);
Try the below code:
<?php
$myposts = get_posts(array(
'showposts' => -1,
'post_type' => 'Products',
'tax_query' => array(
array(
'taxonomy' => 'products_cat',
'field' => 'slug',
'terms' => 'oil'
);
)
)
);
foreach ($myposts as $mypost) {
echo $mypost->post_title . '<br/>';
echo $mypost->post_content . '<br/>';
echo get_the_post_thumbnail( $mypost->ID );
}
?>
$loop = new WP_Query(array(
'posts_per_page' => 10,
'post_type' => 'product_type',
'order' => 'DESC',
'orderby' => 'id',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'your_texonomy',
'field' => 'slug',
'terms' => 'Oil',
),
),));
You need to try the following parameters in query.
$args = array(
'post_status' = 'publish',
'tax_query' = array(
'taxonomy' => 'categories',
'field' => 'id',
'term' => 'category id here'
)
);
$the_query = wp_query($args);

Categories