how display post on subcategory - wordpress

I want to display posts in the subcategory but don't know what to do. But this code does not show the post in the category but the entire post in the category. Can you help me?
<?php
$query = new WP_Query( array(
'post_type' => 'hotcheck',
'topics__in' => array($cat->term_id),
) );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="entry">
<h2 class="title"><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>

Please try below code
$args = array(
'post_type' => 'hotcheck',
'tax_query' => array(
array(
'taxonomy' => 'category', // Add your taxonomy name here
'field' => 'term_id',
'terms' => $cat->term_id,
),
),
);
$query = new WP_Query($args);
Hope its work for you Thanks!

Related

Advanced custom field query in WordPress

I am trying to list all the post while advanced custom field value ispremium=>yes and post issticky in my WordPress site. I have following code in my page. It is listing all the post but not checking ispremium=>yes and issticky post rather showing all the posts.
What's wrong in my code?
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => 'ispremium',
'meta_value' => 'yes'
);
// query
$the_query = new WP_Query( $args and is_sticky());
?>
<?php if($the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('event_thumbnail'); ?>" />
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Try this:
$args = array(
'posts_per_page' => -1,
'post__in' => get_option( 'sticky_posts' ),
'post_type' => 'post',
'meta_key' => 'ispremium',
'meta_value' => 'yes',
'ignore_sticky_posts' => 1,
);
$the_query = new WP_Query( $args );

How to display posts with Category named news

I just created a new page template and want to print all posts with category news only.
Used this code based on wordpress codex when it works.
<?php
query_posts(
array (
'post_type' => 'post',
'category_name' => 'news',
'category' => 1,
'posts_per_page' => 3 )
);
// The Loop
while ( have_posts() ) : the_post();
the_title();
the_content();
endwhile;
// Reset Query
wp_reset_query();
?>
How can wrap the title into an h1 tag and the content into a div box?
I tried this but it is giving me syntax error:
<h1><?php the_title(); ?></h1>
Hope you can help.
<?php global $post;
$args = array(
'posts_per_page' => 3,
'post_type' => 'post',
'category' => 1
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
the_title( '<h1 class="entry-title">', '</h1>' );
endforeach;
wp_reset_postdata();
?>
I think you should remove category = 1 in a parameter of the arguments.
<?php
global $post;
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'offset'=> 1,
'category_name' => 'news'
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
the_title();
the_content();
endforeach;
wp_reset_postdata();
?>
Note: Please use slug of category in category_name. Also you have syntax error in
<?php the_content(); ?>"> //removed ">
Hope this will work for you. Thank you
I have added div tag for content & h1 tag for title & updated your code.
Please find your updated code:
<?php
query_posts(
array (
'post_type' => 'post',
'category_name' => 'news',
'posts_per_page' => 3
)
);
// The Loop
while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<div class="wrap">
<?php the_content(); ?>
</div>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
Hope, this will help you.

Custom Post Type showing even when taxonomy is blank

I am using Custom Post Type to get its contents by using Custom Taxonomy in different pages.
However, the contents are being displayed even if the post type has no taxonomy.
P.S. I am using ACV as well to get the fields of Taxonomy
Here's the code:
<?php if( get_field('review_category_name') ):
$taxonomy_slug = get_field('review_category_name'); //Advanced Custom Field here
endif; ?>
<?php $args = array( 'post_type' => 'reviews','tax_query' => array(
'taxonomy' => 'reviews_category',
'field' => 'slug',
'terms' => $taxonomy_slug,
), 'posts_per_page' => 5 );
$loop = new WP_Query( $args ); ?>
<div class="">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
Am I missing anything here?
$taxonomy_slug is a variable to get the custom post type slug.
Wrap everything in the get_field('review_category_name') if check:
<?php if( get_field('review_category_name') ): ?>
<?php
$taxonomy_slug = get_field('review_category_name'); //Advanced Custom Field here
$args = array( 'post_type' => 'reviews','tax_query' => array(
'taxonomy' => 'reviews_category',
'field' => 'slug',
'terms' => $taxonomy_slug,
), 'posts_per_page' => 5 );
$loop = new WP_Query( $args );
?>
<div class="">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
PS your example was missing the endwhile

Show posts from custom taxonomy

Update 2
Adding name as field instead of the slug and adding the_title() just give me an echo of the page title...
$args = array(
'post_type' => 'feestlocaties',
'showposts' => '3',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'locatie',
'field' => 'name',
'terms' => the_title(),
),
),
);
Update Jonnhyd23's code worked like a charm!! Thanks!
Is there a way you can make the terms dynamic? Like the title is Amsterdam can I do something like 'terms' => '<?php the_title(); ?>' or something like that?
I've been going at this for the last couple of hours. Maybe someone here can help me?
I want to show specif posts from a custom taxonomy in a loop.
This is the situation:
custom taxonomy: feestlocaties
And the the posts i want to show have Amsterdam selected (checked) (like categories).
Code i tried:
<div id="main-filter">
<!-- Start the Loop. -->
<?php $args = array(
'post_type' => 'feestlocaties',
'tax_query' => array(
array(
'taxonomy' => 'locatie',
'field' => 'slug',
'terms' => 'amsterdam',
),
),
); ?>
<?php $query = new WP_Query( $args ); ?>
<?php if( $query->have_posts() ): while( $query->have_posts() ): $query->the_post(); ?>
<!-- Test if the current post is in category 3. -->
<!-- If it is, the div box is given the CSS class "post-cat-three". -->
<!-- Otherwise, the div box is given the CSS class "post". -->
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="container post-item">
<div class="col-sm-3 no-padding">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php the_post_thumbnail(array(400,355)); // Declare pixel size you need inside the array ?>
<?php endif; ?>
</div>
<div class="col-sm-9 no-padding">
<h1 class="overzicht"><?php the_title(); ?></h1>
<?php html5wp_excerpt('html5wp_index'); ?>
<div class="col-sm-12 no-padding loop-overzicht">
<?php $prijs = get_Field('vanaf_prijs'); ?>
<?php $pers = get_Field('aantal_personen'); ?>
<?php $time = get_Field('tijdsduur'); ?>
<ul class="loop-opsomming text-right">
<li><?php echo '<i class="fa fa-euro"></i>Vanaf ' . $prijs . ' p.p.' ?></li>
<li><?php echo '<i class="fa fa-group"></i>Vanaf ' . $pers . ' personen' ?></li>
<li><?php echo '<i class="fa fa-clock-o"></i>Vanaf ' . $time . ' uur' ?></li>
</ul>
</div>
</div>
</div>
</a>
<?php wp_pagenavi(); ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
</div>
But nothing is showing. Any help would be great. Thanks!
Are you just showing the arguments you're using for WP_Query, or is this all of your code? Try using the tax_query parameter.
$args = array(
'post_type' => 'your_post_type',
'tax_query' => array(
array(
'taxonomy' => 'feestlocaties',
'field' => 'slug',
'terms' => 'amsterdam',
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ): while( $query->have_posts() ): $query->the_post();
//execute code
endwhile; endif; wp_reset_postdata();
So, I fiddeld around abit and this is the code that works for me. Cheers Johnnyd23
<?php $args = array(
'post_type' => 'feestlocaties',
'showposts' => '3',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'locatie',
'field' => 'name',
'terms' => get_the_title(),
),
),
); ?>
<?php $query = new WP_Query( $args ); ?>
<?php if( $query->have_posts() ): while( $query->have_posts() ): $query->the_post(); ?>
This will make the title the post dynamicly in the WP_Query.

Display two posts from each post type with wp_query

I have used the following code to display the one post from each post.
$post_types = array('a', 's','d','f','g');//post type names
foreach( $post_types as $post_type) :
// The Query
$the_query = new WP_Query( array( 'post_type' => $post_type,
'orderby' => 'post_date',
'order' => 'DESC',
));
// The Loop
?>
<?php
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="issue-content masonItem <?php echo $post_type; ?>">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id() );
$resizedUrl = get_bloginfo('template_url')."/libs/timthumb.php?src=".$url."&w=327&h=204&zc=1"; ?>
<img src="<?php echo $resizedUrl; ?>" alt="<?php the_title(); ?>" class="masonry-thumb" />
<?php get_template_part( 'content-issues', 'page' );?>
</div><!--issue-content--><!--Mason Item-->
<?php endif; ?>
<?php endwhile;
Now i want to display 2 posts from each post type. How can i do this ? I got the $post_count idea. But i cant know use it in my code.
Try this :
'posts_per_page' => 2,
You try This
$the_query = new WP_Query( array( 'post_type' => $post_type,'orderby' => 'post_date','posts_per_page' => 2,'order' => 'DESC'));

Resources