how to query_posts by category slug - wordpress

I am using the following code to list some pages on my wordpress site:
$args = array( 'posts_per_page' => 12, 'order'=> 'ASC', 'post_type' => 'tcp_product', 'paged' => $paged);
?>
<?php query_posts($args); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink(); ?>" id="prod-link">
<?php if( has_sub_field('images') ): ?>
<?php $img = get_sub_field('image') ?>
<img src="<?php echo $img['sizes']['product-index-pic'] ?>" />
<?php endif; ?>
</a>
<?php endwhile; ?>
<!-- #posts -->
<div class="pagination">
<?php posts_nav_link( ' ', '<img src="' . get_bloginfo('template_url') . '/assets/images/prev.jpg" />', '<img src="' . get_bloginfo('template_url') . '/assets/images/next.jpg" />' ); ?>
</div>
<!-- .pagination -->
I am wondering if there would be a way to limit those based on a certain category slug?
Thanks in advance

You can use the variable category_name like this:
$args = array( 'category_name' => ***YOUR CATEGORY SLUG***, 'posts_per_page' => 12, 'order'=> 'ASC', 'post_type' => 'tcp_product', 'paged' => $paged);

tax_query is used to get the posts associated with certain taxonomy.
{tax} (string) - use taxonomy slug. Deprecated as of Version 3.1 in favor of 'tax_query'.
tax_query (array) - use taxonomy parameters (available with Version 3.1).
taxonomy (string) - Taxonomy.
field (string) - Select taxonomy term by ('id' or 'slug')
terms (int/string/array) - Taxonomy term(s).
include_children (boolean) - Whether or not to include children for hierarchical taxonomies. Defaults to true.
operator (string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND'.
$args = array(
'post_type' => 'tcp_product',
'posts_per_page' => 12,
'tax_query' => array(
array(
'taxonomy' => 'tcp_product_taxonomy',
'field' => 'slug',
'terms' => 'your-cat-slug'
)
)
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post;
// do something
}
}

Related

Related post by custom field

I use wordpress acf custom post type to create my custop post type.
this code (in my custop post type page php) to display the related post of a taxonomy type, now i want to display by Custom Field instead of taxonomy. Thank you for helping me solve my problem
<?php
global $post;
$current_post_type = get_post_type( $post );
$args = array(
'posts_per_page' => 3,
'order' => 'DESC',
'orderby' => 'ID',
'post_type' => $current_post_type,
'post__not_in' => array( $post->ID )
);
$rel_query = new WP_Query( $args );
if( $rel_query->have_posts() ) :
?>
<h1 id="recent">Related</h1>
<div id="related" class="group">
<ul class="group">
<?php
// The Loop
while ( $rel_query->have_posts() ) :
$rel_query->the_post();
?>
<li>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark">
<article>
<h1 class="entry-title"><?php the_title() ?></h1>
<div class="name-date"><?php the_time('F j, Y'); ?></div>
<div class="theExcerpt"><?php the_excerpt(); ?></div>
</article>
</a>
</li>
<?php
endwhile;
?>
</ul><!-- .group -->
</div><!-- #related -->
<?php
endif;
// Reset the query
wp_reset_query();
?>
WP_Query Object accept arguments to query custom field values (more info here: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/). So you need the name of your custom field in this example 'location' and then the value you want 'Melbourne' (you will get the value using get_field('location')).
So your args should look like this:
$args = array(
'posts_per_page' => 3,
'order' => 'DESC',
'orderby' => 'ID',
'meta_key' => 'location',
'meta_value' => 'Melbourne'
'post_type' => $current_post_type,
'post__not_in' => array( $post->ID )
);
Hope this helps

How to show custom texonomy's category related post on single post page

I am working with a custom post type name= product and I have a custom taxonomy name= product-category
on the taxonomy-product.php I am showing a single product and related product based on its single product taxonomy term. But something is missing I can't figure it out.
here is my code.
<?php
//related products
$terms = get_the_terms( $post->ID , 'product-category', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product-category',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'AND' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 3,
'ignore_sticky_posts' => 1,
'orderby' => 'rand',
'post__not_in'=>array($post->ID)
) );
if($second_query->have_posts()) {
while ($second_query->have_posts() ) : $second_query->the_post(); ?>
<div class="single_related">
<?php if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail( 'related_sm', array('alt' => get_the_title()) ); ?>
<?php } else { ?>
<?php the_title(); ?>
<?php } ?>
</div>
<?php endwhile; wp_reset_query();
}?>
I have also tried
https://www.banna360.com/display-related-post-product-based-taxonomy-wordpress/
https://code.tutsplus.com/tutorials/show-wordpress-related-posts-with-taxonomy-and-a-custom-post-type--cms-32303

Wordpress display custom post type with custom taxonomy and current term

Difficult this one:
I'm trying to all posts from a custom post type 'specialisaties' with the custom taxonomy 'specialismen' and need to load the current loaded 'term' from the URL.
Currently I have this code that outputs the term 'superpower' .
<?php $loop = new WP_Query(array('post_type'=>'specialisaties', 'taxonomy'=>'specialismen', 'term' => 'superpower', 'orderby' => 'menu_order', 'order' => 'asc')); ?>
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<h1><?php the_title() ?></h1>
<?php the_content() ?>
<?php endwhile; ?>
This loads the specific post with the term 'superpower'. How do I fetch the 'term' dynamically from the URL I'm loading?
Thanks in advance,
Fixed it with get_term_by .
<?php $term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') ); ?>
<?php $loop = new WP_Query(array('post_type'=>'specialisaties', 'taxonomy'=>'specialismen', 'term' => $term->name, 'orderby' => 'menu_order', 'order' => 'asc')); ?>

Error trying to get the taxonomy term name

Hello—so what I'm attempting is to get 2 posts from 2 taxonomy terms, I've got that part done, where I'm borking up is trying to display the term name above the posts. I've done this before using a foreach loop but no matter what I change, I keep getting the error that I've supplied invalid arguments for the loop. After a lot of googling, I'm a bit lost and wondering if you folks have some guidance for me?
$current_post_id = get_posts( array(
'post_type' => 'issue',
'posts_per_page' => 1,
'fields' => 'ids',
) );
$terms = get_the_terms( $current_post_id, 'department' );
foreach ( $terms as $term ) {
$args = array(
'connected_type' => 'posts_to_issues',
'connected_direction' => 'to',
'connected_items' => $current_post_id,
'post_type' => 'post',
'posts_per_page' => 2,
'tax_query' => array(
array(
'taxonomy' => 'department',
'field' => 'slug',
'terms' => array( 'washington-watch', 'equipment-spotlight'),
)
),
);
$sidebar_query = new WP_Query( $args );
while ( $sidebar_query->have_posts() ) : $sidebar_query->the_post(); ?>
<?php echo '<h2>' . $term->name . '</h2>' ?>
<figure>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'sidebar-thumb-med' ); ?>
</a>
</figure>
<h4><?php the_title(); ?></h4>
<?php the_excerpt();
endwhile;
}
// Restore global post data
wp_reset_query();
$current_post_id is an array with one item (because passing in a return fields argument returns an array). You need to refer to that one item in the following cases:
$terms = get_the_terms( $current_post_id[0], 'department' );
...and...
'connected_items' => $current_post_id[0],

Show only post of custom category

I have a custom post types called photo gallery. In that post type I have registered a taxonomy called video. Then i have created two category under video called 'personal' & 'commercial'. Now i want to show only the commercial category post to a page section. How can I do that? Here is the code I have tried but not working
<?php
$args = array(
'post_type'=>'photo_gallerys',
'post_status'=>'publish',
'video'=>'commercial',
'posts_per_page'=>-1,
'paged'=>get_query_var('paged')
);
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Try this code hope you'll find your solution
<?php
$tax_post_args = array(
'post_type' => 'your post type name',
'posts_per_page' => 999,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'your taxonomy name',
'field' => 'id',
'terms' => your category id
)
)
);
$tax_post_qry = new WP_Query($tax_post_args);
while($tax_post_qry->have_posts()) :
$tax_post_qry->the_post();
the_title();
endwhile;
?>
if you want to get posts from category slug then use this code in tax_query array
'field' => 'slug',
'terms' => 'your category slug'
use 'cat' => "your_cat_id in your wp query argument.
for example if I want to display post for only commercial category and commercial have category id 21, then I will write query to display post like this:
<?php
$args = array(
'post_type'=>'photo_gallerys',
'post_status'=>'publish',
'video'=>'commercial',
'cat' => 21,
'posts_per_page'=>-1,
'paged'=>get_query_var('paged')
);

Resources