How to do with this code only show the "related posts by category"?
This code is inside the "single.php"
I did not stackoverflow but could not find a solution. Can anyone help me?
<?php
$postsPerPageq = 5;
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'slug',
'terms' => 'top-audio'
)
)
);
$topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post();
?>
<h3><?php the_title();?></h3>
<?php endwhile; wp_reset_postdata();?>
First, you need to grab current post terms using this function
get_the_terms ( get_the_id(), 'your_custom_taxonomy' );
then you grab the terms slug for tax_query in your WP_Query.
$postsPerPageq = 5;
$terms_slugs = array();
//get the current post terms slug
$terms = get_the_terms( get_the_id(), 'audiotop' );
foreach ( $terms as $term) {
$terms_slugs[] = $term->slug;
}
//WP_Query args
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'slug',
'terms' => $terms_slugs,
)
)
);
i hope it your solve your problem.
Add category name in your allargw array value..
<?php
$postsPerPageq = 5;
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'category_name'=>'Your category name',
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'slug',
'terms' => 'top-audio'
)
)
);
$topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post();
?>
<h3><?php the_title();?></h3>
Hope this helps to you!!
Updated answer:
If you want add category automatically, use like this..
wp_set_object_terms( 'your_post_id', 'your_category_name', 'your_taxonomy_name');
You can try this..
if you want to retrieve posts by simple posts category then check this code:
<?php
$category = get_the_category($post->ID);
$cat_id = $category[0]->term_id;
$postsPerPageq = 5;
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'cat' => $cat_id
);
$topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post(); ?>
<h3><?php the_title();?></h3>
<?php endwhile; wp_reset_postdata();?>
else if you want to retrieve posts by custom taxonomy then check the code below:
<?php
$term_list = wp_get_post_terms($post->ID, 'audiotop', array("fields" => "all"));
$term_id = $term_list[0]->term_id ;
$postsPerPageq = 5;
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'id',
'terms' => $term_id
)
)
);
$topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post(); ?>
<h3><?php the_title();?></h3>
<?php endwhile; wp_reset_postdata();?>
Hope, this will help you.
Related
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'm trying to overwrite the default 'Blog pages show at most' which is set to 5 posts. I have a custom post type called 'FAQs', which has the argument 'posts_per_page' => 999 in the query for getting all the posts of this type, however I can't seem to override the default restriction in the WordPress setting. My code for the FAQ query is below, which works on my local machine (MAMP) but not when I upload it to live. how do I show all posts of that type?
<?php
wp_reset_query();
// Query for getting custom taxonomy 'FAQ Type' of custom post type 'FAQs'
$cat_args = array (
'taxonomy' => 'faq_type',
'exclude' => array(12),
'posts_per_page' => 999,
//'show_all' => true,
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_categories ( $cat_args );
foreach ( $categories as $category ) {
//wp_reset_query();
$cat_query = null;
// Query for getting posts of custom post type 'FAQs'
$args = array (
'post_type' => 'faq',
'faq_type' => $category->slug,
'posts_per_page' => 999,
//'show_all' => true,
'orderby' => 'simple_page_ordering_is_sortable',
);
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) { ?>
<?php echo "<h2>". $category->name ."</h2>"; ?>
<ul id="resident-accordion" class="accordion white-bg-accordion" data-accordion data-allow-all-closed="true" role="tablist">
<?php
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
?>
<li class="accordion-item faq-content <?php //if ($firstLoop === true) { echo "is-active"; }?>" data-accordion-item>
<?php the_title(); ?>
<div class="accordion-content" data-tab-content>
<?php the_content(); ?>
</div>
</li>
<?php
} //wp_reset_query();
wp_reset_postdata(); //End WHILE
echo "</ul>";
} //End IF
wp_reset_postdata();
//wp_reset_query();
} //End FOR
?>
You can try to use this code below :
<?php
$cat_args = array(
'taxonomy' => 'faq_type',
'exclude' => array(7),
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_terms( $cat_args );
foreach ( $categories as $category )
{
$args = array(
'post_type' => 'faq',
'posts_per_page' => -1, // load all posts
'orderby' => 'simple_page_ordering_is_sortable',
'tax_query' => array(
array(
'taxonomy' => 'faq_type',
'field' => 'slug',
'terms' => $category->slug
)
)
);
$cat_query = new WP_Query( $args );
// enter the rest of your code below
}
Or you can use get_posts() to receive posts list.
<?php
$cat_args = array(
'taxonomy' => 'faq_type',
'exclude' => array(7),
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_terms( $cat_args );
foreach ( $categories as $category )
{
$posts = get_posts(array(
'numberposts' => -1, // get all posts.
'tax_query' => array(
array(
'taxonomy' => 'faq_type',
'field' => 'slug',
'terms' => $category->slug,
'operator' => 'IN',
),
),
'post_type' => 'faq',
));
// enter the rest of your code below
}
Cheers!
You found your answer by now I hope, but you were so close.
Instead of:
'posts_per_page' => 999,
try:
'posts_per_page' => -1,
see: Pagination Parameters
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;
}
?>
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'),
)
)
);
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);