I have loop which outputs last 3 posts:
<?php
$args = array( 'numberposts' => 3 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
/////////insert smth
<?php endforeach; ?>
I need to make sure that post category is equal to "vakancy", can i filter only for posts only with this category or i will have to use (if!=) ?
Sure its easy: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
<?php
$args = array( 'numberposts' => 3, 'category_name' => 'vakancy' );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
/////////insert smth
<?php endforeach; ?>
Related
I want to display all members of my site.
As I understand the WooCommerce Membership plugin, the members are stored as custom post type (https://docs.woocommerce.com/document/woocommerce-memberships-data-structure/#section-4) and I could get them with get_posts().
I tried to do so with the following code:
<ul class="my-5">
<?php
global $post;
$args = array( 'post_type' => 'wc_user_membership' );
$posts = get_posts( $args );
foreach ( $posts as $post ) :
setup_postdata( $post ); ?>
<li><?php echo $post->ID; ?></li>
<?php endforeach; wp_reset_postdata(); ?>
</ul>
Unfortunately without any result.
Is there anything wrong with my code?
Updated:
Try to add in your $args array 'numberposts' => -1 and 'post_status' => 'wcm-active' so your code will be:
<ul class="my-5">
<?php
$posts = get_posts( array(
'post_type' => 'wc_user_membership',
'post_status' => 'wcm-active',
'numberposts' => -1,
) );
foreach ( $posts as $post ) :
setup_postdata( $post ); ?>
<li><?php echo $post->ID; ?></li>
<?php endforeach; wp_reset_postdata(); ?>
</ul>
You don't need global $post; as you are using $post in the foreach loop.
Tested and works.
I have following code
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 3,
'cat_id'=> 5,
));
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I want to show category whose id is 5 on my bog page. But this is showing other categories as well like 4 & 3.
Similarity issue is for archive page as well. Where i am wrong?
check this, i think this is helpful for you.
<?php
$posts = get_posts('category=5&orderby=rand&numberposts=5');
foreach($posts as $post) {
?>
<?php the_title(); ?>
<?php } ?>
Please try with below code :
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
this is working at my end
Try this
<?php
$args3 = array(
'numberposts' => 1,
'cat' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$homepage_post = get_posts($args3);
foreach ($homepage_post as $post) : setup_postdata($post);
// write your code here ...
endforeach;
wp_reset_postdata();
?>
--
Thanks
On single posts i display other posts like this outside the loop
$posts = get_posts('numberposts=200&category='. $category->term_id);
foreach($posts as $post) :
Trying adding
query_posts($query_string . '&orderby=rand');
But however i try it don't do it in random order
Problem was that i didn't put it first in get_posts like
$posts = get_posts('orderby=rand&numberposts=200&category='. $category->term_id);
foreach($posts as $post) :
Try this:
<?php
$args = array( 'posts_per_page' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach ( $rand_posts as $post ) :
setup_postdata( $post ); ?>
<li><?php the_title(); ?></li>
<?php
endforeach;
wp_reset_postdata(); ?>
You can see the codex:
http://codex.wordpress.org/Template_Tags/get_posts
I have this code:
<?php
global $post;
$args = array( 'numberposts' => 4, 'category' => 15 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
The issue is that I want to grab more than just category number 15. Simply add them next to 15 like 15, 18 or "15,18" does not work.
Any ideas?
Thanks
Try this one: it is work in my project.
<?php
global $post;
$args = array( 'numberposts' => 4, 'category' => '15,8');
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
Thanks.
I'm using this in my page template to get posts by their category:
<?php
if (is_page(19)){
?>
<ul>
<?php
global $post;
$args = array( 'category' => 'Testimonial' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
</ul>
<?php } ?>
but it's retrieving all posts instead. Not just the ones labeled Testimonial. Any idea what I'm doing wrong?
'category_name'=>'this cat' also works but isn't printed in the WP docs
Check here : https://developer.wordpress.org/reference/functions/get_posts/
Note: The category parameter needs to be the ID of the category, and
not the category name.
You can use 'category_name' in parameters.
http://codex.wordpress.org/Template_Tags/get_posts
Note: The category_name parameter needs to be a string, in this case, the category name.
add_shortcode( 'seriesposts', 'series_posts' );
function series_posts( $atts )
{ ob_start();
$myseriesoption = get_option( '_myseries', null );
$type = $myseriesoption;
$args=array( 'post_type' => $type, 'post_status' => 'publish', 'posts_per_page' => 5, 'caller_get_posts'=> 1);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post();
echo '<li><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo '</a></li>';
endwhile;
echo '</ul>';
}
wp_reset_query();
return ob_get_clean(); }
//this will generate a shortcode function to be used on your site [seriesposts]
Create a taxonomy field category (field name = post_category) and import it in your template as shown below:
<?php
$categ = get_field('post_category');
$args = array( 'posts_per_page' => 6,
'category_name' => $categ->slug );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
//your code here
<?php endforeach;
wp_reset_postdata();?>