Show only post of custom category - wordpress

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')
);

Related

I want to show post for whose birthday is today

This is the code i have found in deed, please help me as i am new to wordpress
What you can do is create a meta field under a post and store birth date in that field. Now you need meta_query in you post to retrieve the current date posts.
<?php $currentDate = current_time( 'Y-m-d' );
$args = array(
'post_type' => 'biography',
'meta_key' => 'date_of_birth',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_of_birth',
'value' => $currentDate,
'compare' => 'IN',
),
),
);
$query = new WP_Query( $args );
if ( $query ->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $query ->have_posts() ) : $query ->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Note :
1) "birth_date" is the key of the meta field that should be created under the post.
2) Format of current date and stored date should be same.

get posts within get_terms loop

I have a basic loop set up to display all terms in a custom taxonomy.
<?php
$workshops = get_terms( 'workshop', array(
'orderby' => 'name',
'hide_empty' => 0,
) );
foreach ( $workshops as $workshop ) { ?>
<h3><?php echo $workshop->name; ?></h3>
<?php echo term_description($workshop); ?>
<?php } ?>
How can I display all posts for each respective term within that loop?
For example..
Taxonomy is Movies. Terms are Comedy, Horror, etc.
I'd like output to be
Comedy
Description for comedy term
Movie 1
Movie 2
Horror
Description for horror term
Movie 3
Movie 4
Thanks! Rich
First off, you are using a deprecated version of get_terms so we should fix that first:
$workshops = get_terms( array(
'taxonomy' => 'workshop',
'orderby' => 'name',
'hide_empty' => false
) );
Then, within your term loop, you need to create another query to grab all the posts that fall under the term:
$query = new WP_Query( array(
'post_type' => 'post', // Or your custom post type's slug
'posts_per_page' => -1, // Do not paginate posts
'tax_query' => array(
array(
'taxonomy' => 'workshop',
'field' => 'term_id',
'terms' => $workshop->term_id
)
)
) );
Finally, still within your term loop, write another loop to build the lists of posts:
<?php if ( $query->have_posts() ): ?>
<ul class="term-post-list" id="term-<?php echo $workshop->term_id; ?>-posts">
<?php while ( $query->have_posts() ): $query->the_post(); ?>
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title(); ?>
</li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
<?php endif; ?>

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')); ?>

how to query_posts by category slug

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
}
}

Cant' Display Custom Post Type Title Base on Taxonomy Terms

Trying to list all Custom Post Type titles based on filtered Custom Taxonomy Terms I am getting the list of all post titles instead of getting the list of Queried post. Here is the code I am using:
<?php
$loop = new WP_Query(
array(
'post_type' => 'photos',
'technique' => 'zevar',
'post_child' => 0,
'posts_per_page' => 10
)
);
?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
As you can see I have a Custom Post Type called "photos" and Custom Taxonomy registered as "technique". Under Taxonomy "technique" I have some terms which one of them is "zevar". Can you please let me know what I am doing wrong here?
Your taxonomy query is not correct. check following code.
<?php
$loop = new WP_Query(
array(
'post_type' => 'photos',
'tax_query' => array(
array(
'taxonomy' => 'technique',
'field' => 'slug',
'terms' => 'zevar'
)
),
'posts_per_page' => 10
)
);
?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
Reference on how to use taxonomy parameter https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Resources