I am trying to show related posts by taxonomy in single post page on Wordpress. I've used the following code to show posts in the same category, but not the same custom taxonomy. The custom taxonomy I need to use is product_cat
<?php
global $post;
$categories = get_the_category();
$category = $categories[0];
$cat_ID = $category->cat_ID;
$loop = new WP_Query( array( 'post_type' => 'product','post__not_in' => array($post->ID), 'category' => $cat_ID ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
<?php endwhile; ?>
How can I adjust the current code?
Did you tried adding the following argument to your WP_Query array of arguments?
'tax_query' => array(array('taxonomy' => 'product_cat'))
The code will look like this (of course after removing the category parameter):
<?php
global $post;
$loop = new WP_Query( array( 'post_type' => 'product','post__not_in' => array($post->ID), 'tax_query' => array(array('taxonomy' => 'product_cat'))) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
<?php endwhile; ?>
Related
I've got an advanced custom, outputting a chosen taxonomy, which I'm trying to pass into an array inside a WordPress loop.
The loop, which shows a specific taxonomy of my custom post type, is here:
<?php
$loop = new WP_Query( array(
'post_type' => 'portfolio',
'portfolio_category' => 'social-media-marketing',
'posts_per_page' => -1,
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; wp_reset_query(); ?>
I would like to replace the portfolio category with the results from the custom post type, so the user can select which taxonomy to display.
The code I have to pull in the Advanced custom field taxonomy is here:
<?php
$term = get_field('portfolio_category');
if( $term ): ?>
<h2><?php echo $term->slug; ?></h2>
<?php endif; ?>
Both bits of code work separately. I've tried running them both together like this:
<?php
$term = get_field('portfolio_category');
$loop = new WP_Query( array(
'post_type' => 'portfolio',
'portfolio_category' => 'echo $term->slug;',
'posts_per_page' => -1,
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; wp_reset_query(); ?>
As well as a few other things, but I can't seem to get it to display anything... What am I doing wrong??
Change this
'portfolio_category' => 'echo $term->slug;'
To:
'portfolio_category' => $term->slug
You were passing the variable as a string instead of a variable.
Our wordpress post loop combines and displays posts from a specific category and a custom post type. This works, but is not displaying all posts. I believe that the post loop is iterating over the number of posts in the specific category, not the number of posts in the specific category + the number of posts in the custom post type. How can I ensure that the correct number of posts are being displayed?
<?php
/*
Template Name: Articles & Cases
*/
get_header(); ?>
<div class="center-holder">
<div id="content">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title( '<h1>', '</h1>' ); ?>
<?php the_post_thumbnail( 'full' ); ?>
<?php the_content(); ?>
<?php if ( $cats = get_field( 'category' ) ) : ?>
<?php
$args = array(
'post_type' => array( 'post' ),
'category__in' => $cats,
'fields' => 'ids',
);
$articles = new WP_Query( $args );
wp_reset_postdata();
$args = array(
'post_type' => array( 'case_study' ),
'fields' => 'ids',
);
$case_study = new WP_Query( $args );
wp_reset_postdata();
$all_posts_ids = array_merge( $articles->posts, $case_study->posts );
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array( 'post', 'case_study' ),
'post__in' => $all_posts_ids,
'paged' => $paged,
);
query_posts( $args );
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blocks/content', get_post_type() ); ?>
<?php endwhile; ?>
<?php get_template_part( 'blocks/pager' ); ?>
<?php else: ?>
<?php get_template_part( 'blocks/not_found' ); ?>
<?php endif; wp_reset_query(); ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php get_sidebar( 'blog' ); ?>
</div>
<?php get_footer(); ?>
There is a number of things you're doing wrong.
You are using a page template, and then are looping using the loop.
This probably pulls all posts, as the last fallback for page template is index.php (as seen in the diagram here).
Then you are making 3 additional queries while in the loop (so for every post you loop you make 3 extra queries).
And the last query, you are using query_posts() which is overriding the main query. Just don't ever use that.
So the plan of attack should be:
What do I want to show?
How and where do I want to show it?
What do I need to do to achieve this?
Start writing things needed to achieve this.
???
Profit!!!
If you want to control specific taxonomy, use $taxonomy.php template (with $taxonomy being the name of the taxonomy.).
Hope this helps.
I've got Two custom Post Types set up in Wordpress, One being called Products and the other Suppliers. Slugs for these are 'product' and one being 'supplier'.
These two post types share a custom taxonomy called Suppliers which slug is 'supplier-tax'.This then has lots of children which are the different suppliers.
Basically, What I am trying to do is when you are on a single post page for a product, I need to pull in a post for the supplier as well. I thought that the best way to do this with how I have set it up is to select the appropiate supplier from the supplier taxonomy when on the product page. And this then queries and gets post from the 'Supplier' Post ttype with the same selected Taxonomy.
I wrote this query which brings in posts from the taxonomy, however It needs me to tell it which taxonomy and which slug etc to bring in plus it doesnt query the different post type which makes it useless, however it was a start:
<?php
$the_query = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
'taxonomy' => 'supplier-tax',
'field' => 'slug',
'terms' => 'supplier_1',
),
) );
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_post_thumbnail('full'); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
I've tried to adapt and include parts from queries I've fond on previous sources but I can't seem to crack it. This is my attempt at it:
<?php
$terms = wp_get_post_terms( $post->ID, 'supplier-tax' );
if($terms){
$supplier_terms = array();
foreach ($terms as $term){
$supplier_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
'post_type' => 'supplier',
'tax_query' => array(
array(
'taxonomy' => 'supplier-tax',
'field' => 'slug',
'terms' => $supplier_terms, //the taxonomy terms I'd like to dynamically query
'posts_per_page' => '-1'
),
),
'orderby' => 'title',
'order' => 'ASC'
) );
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?>
<?php the_title(); ?>"><?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
}
?>
Has anyone got any ideas on what I'm doing wrong or how I can make this work?
I managed to find a solution to my problem, whilst I don't think it is the cleanest markup, it works and does the job.
<?php
$the_query = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
'taxonomy' => 'supplier-tax',
),
) );
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$terms = get_the_terms( $post->ID, 'supplier-tax');
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
echo $termID[0];
?>
<?php
$my_query2 = new WP_Query( array(
'post_type' => 'supplier',
'tax_query' => array(
'field' => 'slug',
'terms' => '$termID',
),
) ); ?>
<?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
<?php the_title(); ?>
<?php the_post_thumbnail('full'); ?>
<?php the_content(); ?>
<?php endwhile; ?>
In wordpress I have a page template called news that I want to display all the posts from one category - 'News'. I don't want to use the category.php because there is a massive blog on the site already.
query_posts('cat=145');
while ( have_posts() ) : the_post();
//do something
endwhile;
Works fine but I have read that query_posts has drawbacks (like speed)
I tried doing this but it just showed me nothing:
while ( have_posts() ) : the_post();
if ( in_category( '145' ) ) : //also tried 'News'
//do something
Why doesn't' in_category work here?
please try this code:
$args = array('post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'news' // please pass here you news category slugs
),
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print_r($post);
endwhile;
wp_reset_postdata();
You could WP Query for achieving your requirement.
Documentation: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Example:
<?php
$args = array(
'cat' => 145,
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Try to use get_posts() function:
$get_p_args = array('category'=> 145,'posts_per_page'=>-1);
$myposts = get_posts( $get_p_args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div>
<?php the_title(); ?>
</div>
<?php endforeach;
wp_reset_postdata();?>
Try this:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $wp_query;
$args = array_merge( $wp_query->query_vars, array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 6, // limit of posts
'post_status' => 'publish',
'orderby' => 'publish_date',
'order' => 'DESC',
'lang' => 'en', // use language slug in the query
) );
query_posts( $args );
I am using a custom post type in my wordpress theme, and I need help with the loop. Here is my code:
<?php $loop = new WP_Query( array( 'post_type' => 'magazine', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail( 'magazine' ); ?>
<h2><?php the_title( ); ?></h2>
<?php the_content;?>
</li>
<?php endwhile; ?>
This returns the 10 latest posts in the custom field "magazine". I want it to only display the parents in the custom field "magazine". Just like pages, my custom fields have attributes, so you can select a hierarchy (parent/child). I want to edit the loop so it only returns the parents (the latest issues of the magazine, not the articles within each issue) Does anyone know how to do that using the wordpress loop above?
Just add 'post_parent' => 0 to the args array.
<?php $loop = new WP_Query( array( 'post_type' => 'magazine', 'posts_per_page' => 10, 'post_parent' => 0 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail( 'magazine' ); ?>
<h2><?php the_title( ); ?></h2>
<?php the_content;?>
</li>
<?php endwhile; ?>