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);
Related
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'?
I have a simple taxonomy-product_cat.php page, where I am trying to display only the products in the current category. Right now, it's displaying all products, not just the ones in the current category. Here is my code:
<ul class="products">
<?php
$current_cat_id = $wp_query->get_queried_object()->term_id;
$args = array(
'taxonomy' => 'product_cat',
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $current_cat_id,
'operator' => 'IN'
)
);
$products = new WP_Query( $args );
while ( $products->have_posts() ) : $products->the_post();
echo '<li><div class="product__preview"><img src="' . get_the_post_thumbnail_url() . '"></div><span>' . get_the_title() . '</span></li>';
endwhile;
wp_reset_query();
?>
</ul>
My client keeps changing the names/slugs of the category, so I need to get the products by category ID. Any idea why this is generating all products, instead of just the ones in the current category?
Update 2
The tax query need to have 2 arrays embedded in each other: 'tax_query' => array( array(
Removed the first unneeded 'taxonomy' => 'product_cat',
Replaced 'terms' => $current_cat_id, by 'terms' => array( get_queried_object()->term_id ),
Replaced wp_reset_query(); by wp_reset_postdata();
Your code will be:
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( get_queried_object()->term_id ),
) )
) );
while ( $query->have_posts() ) : $query->the_post();
echo '<li><div class="product__preview"><img src="' . get_the_post_thumbnail_url() . '"></div><span>' . get_the_title() . '</span></li>';
endwhile;
wp_reset_postdata();
Tested and works
although its correct answer you can get it through category slug.
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'category slug here',
);
$products_all = new WP_Query( $args );
version tested 4.6.1
$terms = wp_get_post_terms( $product->id, 'product_cat' );
$args_listing = array(
'post_type' => 'product',
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $terms[0]->term_id,
) ),
'orderby'=> 'date',
'order'=> "desc",
'posts_per_page' => 10,
'paged' => $paged
);
I think you can try this. its working for me.
I use woocommerce plugin.
I have products listed by category. I need to list them by price.
I tried this code below :
$args = array(
'post_type' => 'product',
'meta_key' => '_price',
'orderby' => 'meta_value_num',
'order' => $order,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cid
)
)
);
$my_query = new WP_Query( $args );
The result is not sorted by price, only by id.
Is there any solution?
products listed by category
<?php
$products_category_object= get_queried_object();
$product_category_taxonomy= $products_category_object->taxonomy;
$product_category_term_id= $products_category_object->term_id;
$product_category_name= $products_category_object->name;
$product_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => '_price',
'orderby' => 'meta_value_num', //meta_value Or meta_value_num
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $product_category_taxonomy,
'field' => 'id',
'terms' => $product_category_term_id
)
),
);
$product_my_query = null;
$product_my_query = new WP_Query($product_args);
if( $product_my_query->have_posts() )
{
while ($product_my_query->have_posts()) : $product_my_query->the_post();
echo get_the_title( $post->ID );
endwhile;
}
?>
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().
}
I am trying to display all custom post types of a specific taxonomy in WP.
Here is the code:
<?php
$args = array(
'post_type' => 'team',
'orderby' => 'rand',
'posts_per_page' => -1
);
$trit = new WP_Query($args);
while ($trit->have_posts()) : $trit->the_post();
$post_id = get_the_ID();//POST ID
$terms = get_the_terms($post->ID, 'tax_members' );
...
endwhile;
The script above displays all members of all taxonomies (tax_members). My goal is to display only the members of a specific category...for example: players.
How can I call the taxonomy players inside
$terms = get_the_terms($post->ID, 'tax_members' );
<?php
$queryArr = array(
'post_type' => 'team',
'orderby' => 'rand',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tax_members',
'field' => 'slug',
'terms' => 'players',
'operator' => 'IN'
),
),
);
$trit = get_posts( $queryArr );
?>
Try this
$args = array(
'post_type' => 'team',
'orderby' => 'rand',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tax_members',
'terms' => array('players'),
)
)
);