wordpress get posts of category code example
Example 1: get_posts category
$defaults_param = array(
'numberposts' => -1,
'post_type' => 'post',
'category' => 40 ,
'suppress_filters' => false
);
$get_featured_post = get_posts($defaults_param);
Example 2: get post by category
<section class="films-tabs pd-40" id="portfolio">
<?php $film_genres = get_terms('portfolio_cat'); ?>
<div class="container">
<div class="row">
<ul class="nav nav-tabs nav-justified">
<?php foreach($film_genres as $film_genre) { ?>
<li>
<a href="#<?php echo $film_genre->slug ?>" data-toggle="tab"><?php echo $film_genre->name ?></a>
</li>
<?php } ?>
</ul>
</div>
</div>
<div class="tab-content">
<?php foreach($film_genres as $film_genre) { ?>
<div class="tab-pane" id="<?php echo $film_genre->slug ?>">
<?php
$args = array(
'post_type' => 'portfolio',
'showposts' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_cat',
'field' => 'slug',
'terms' => $film_genre->slug
)
)
);
$films = new WP_Query( $args );
?>
<?php if ( $films->have_posts() ) : ?>
<div class="table">
<div class="container">
<div class="row">
<?php while ( $films->have_posts() ) : $films->the_post(); ?>
<div class="table-cont"><a data-fancybox="gallery" href="<?php echo get_the_post_thumbnail_url();?>".><?php the_post_thumbnail() ?></a></div>
<?php endwhile; ?>
<?php wp_reset_query() ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
<? } ?>
</div>
</section>
Example 3: wordpress display posts from specific category on page
The argument isn't category, it is cat. Your query fails because you are using an argument that doesn't exist.
$args = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 6,
'cat' => '3',
'paged' => get_query_var('paged'),
'post_parent' => $parent
);
$q = new WP_Query($args);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
}
}
Notice that I have converted your query_posts() into a new WP_Query object. Do not use query_posts(), ever. Even the Codex states so.
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of pre_get_posts hook, for this purpose.
http:
Also note that I removed unnecessary PHP opening and closing tags and formatted the code for better readability. That alternative control structure syntax is a formula for failure, in my experience.