Wordpress random post inside while loop - wordpress

I am using a while loop
while (have_posts()) : the_post();
to get all the post and displaying a page. How to randomise the post. But the problem is, in the page I am working on there is no sign of 'query'. Also I tried to use some solutions mentioned but no result.

You can alter the main query with query_posts($args) this way:
// Alter the query
query_posts( 'orderby=RAND' );
while ( have_posts() ) : the_post();
// Do your stuff inside the loop
endwhile;
// Reset the query
wp_reset_query();

Related

Display custom post using ID

In my theme, I have a custom post type called sliders. Here the user is allowed to upload images in a meta-box. So, the images are saved as meta data in wp_post_meta table.
Now what I want to do is, displaying a slider using its ID.
I did like following but no result
$my_query = new WP_Query('post_type=sliders&p=411');
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
the_post();
endwhile;
}
This doesn't display anything. No even errors. Anyway, If I used the_title() instead of the_post(), it shows the title of the slider fine. Same for the the_author() It shows the author without error.
Why is this weird ?
According to Codex the_post(); :
the_post(); iterate the post index in The Loop. Retrieves the next
post, sets up the post, sets the 'in the loop' property to true.
This function does not return any values.
Example for how we use this function:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
he_title();
the_content();
}
}
?>
You didn't explain well you question. anyway to display your metabox just use API function
echo get_post_meta( $id, 'metabox_name', true );

How to print a wordpress query?

I have a while loop in my wordpress site. How to print its query details ?
while (have_posts()) : the_post();
I tried, but not working:
print_r($GLOBALS['wp_query']->request);
Add this in your functions.php then add ?debug=sql after the url, for example
http://someUrl.com/something?debug=sql
Or
http://localhost/localCopy?debug=sql
Also <?php echo $GLOBALS['wp_query']->request; ?> should work, just put it right after the loop if you didn't it before.

Display specific post format with limit number of post

I create a loop in Wordpress with the condition like this:
Display a specific post format (example: video)
Limiting the post number. In this case, I only want to display 2 posts.
Here my code:
<?php $i = 1; if (have_posts()) : while (have_posts() && $i < 3) : the_post(); ?>
<?php get_template_part( 'content-video', get_post_format() ); ?>
<?php $i++; endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
I already have file name content-video.php.
Unfortunately, the loop does not work. Only displayed the first post, not a specific post (video post format) with template from content-video.php.
Appreciate for any help, or alternative code. Thanks in advance.
My remarks about your code:
I think you have bad wrapping for if else while statements. You are missing an endif at the end in the case your approach is correct.
Why use inside your code the i variable since you can customize any query in WP, especially for number of post using the parameter posts_per_page.
Why not to use the loop inside content-video.php and write only:get_template_part('content-video', get_post_format()); Like in single.php and loop-single.php the themes provided with wordpress installation (twenty themes) .
Good luck
the best way would be to get posts of "video" post format from a query, but I'm not sure how:)
As a workaround you could do this inside the loop:
if (!has_post_format('video'))
continue;

Get a specific page's excerpt within the loop Wordpress

I am trying to create a loop that loads a random image from any posts, whilst also retrieving the excerpt of a specific page. I have done the random post part, but cannot get it to retrieve the page excerpt... I think I may need to query the pages in their own loop but I'm not sure how to do this. I have installed the function to get page excerpt support etc. but I think I am doing something wrong within the loop, any help would be appreciated.
<div class="postimage">
<?php if (have_posts()) :
query_posts('showposts=1&orderby=rand');
while (have_posts()) : the_post(); ?>
<?php the_post_thumbnail('blog-post-image'); ?>
<div class="borderimage"></div>
<div class="tagline"><h1><?php the_excerpt('$page_id=8'); ?> </h1>
</div>
</div>
</div>
<?php endwhile; else : endif; ?>
query_posts replaces the global $wp_query, which you don't want to do since you want to keep that query for your page. Try this instead...
if (have_posts()){
while(have_posts()){
the_post(); //global $post now has the page in it
$args = array("posts_per_page"=>1,"orderby"=>"rand");
$random_posts = get_posts($args); //returns an array
$random_post = $random_posts[0];
//do your stuff...
//$post contains the original page
//$random_post contains the random post
}
}

Different number of posts (WordPress)

We have selected to show 5 posts per page in admin panel.
And we want to show 10 posts per page in specific category (for example, "projects" width id=2).
How would we do it?
Change the normal loop to a query post. Like
if ( is_category(2) ){
//The Query
query_posts('posts_per_page=5');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; else:
echo'Nothing here...';
endif;
//Reset Query
wp_reset_query();
}
This should be used in the main loop only. . If you want to create separate Loops outside of the main one, you should create separate WP_Query objects and use those instead.
Cheers
Pali Madra
I had the same problem.
This worked for me:
if ( is_category(2) ){
global $query_string;
query_posts( $query_string . "&posts_per_page=500" );

Resources