Random post order - Wordpress - php

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

Related

Loop throuh all members of site in WordPress/WooCommerce

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.

Wordpress output posts with category

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

Modifying loop to show custom fields just for the active/opened post- Wordpress

I need to modify this code to show get custom fields just for the post on which the user currently is.
// this needs to be modified
<?php
global $post;
$args = array('category' => 37, 'post_type' => 'post' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
// end this needs to be modified
<?php if( get_post_meta($post->ID, "Title", true) ): ?>
<?php echo get_post_meta($post->ID, "Title", true); ?></p>
<?php endif; ?>
Because this will be included, I cannot make it just with get_post_meta.
Many thanks!
This ignores the loop and only shows the custom field for the current page, notice get_the_ID():
<?php if( get_post_meta(get_the_ID(), "Title", true) ): ?>
<p><?php echo get_post_meta(get_the_ID(), "Title", true); ?></p>
<?php endif; ?>
<?php
global $post;
$args = array('category' => 37, 'post_type' => 'post' );
$postslist = get_posts( $args );
foreach ($postslist as $post){
setup_postdata($post);
}
?>
And this will output all custom fields named "Title" from within the loop:
<?php
global $post;
$args = array('category' => 37, 'post_type' => 'post' );
$postslist = get_posts( $args );
foreach ($postslist as $post){
setup_postdata($post);
echo('<p>'.get_post_meta($post->ID, "Title", true).'</p>');
}
?>

WP -- Get posts by category?

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

Wordpress WP_Query, return to current Query

Issue I'm having is returning some data in a sidebar depending on which category the user is in. As of now I'm populating some thumbnails from 'Press' category in the sidebar, which is proving to be problematic. This is due to after I Query (from 'Press' cate) the thumbnails I want to pull, I notice it keeps me from accessing the current 'Blog' cate Query. How do I return back to the current category query?
<?php the_category(', ') ?> // This Returns 'Blog'
<?php
$args = array( 'numberposts' => 2, 'category' => 4);
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div>generic return values</div>
<?php endforeach; ?>
<?php the_category(', ') ?> // This Returns 'Press', I want this to return 'Blog'
A call to wp_reset_query should get everything working for you again:
<?php
$args = array( 'numberposts' => 2, 'category' => 4);
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div>generic return values</div>
<?php endforeach;
// Restore's the loop's post object
wp_reset_query();
?>

Categories