I am using a wordpress query to display 3 posts at random from a custom post type. I am using the following code which is working fine:
<?php $my_query = new WP_Query('post_type=my_post_type&orderby=rand&showposts=3'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
Do Stuff
<?php endwhile; ?>
<?wp_reset_query(); ?>
However, I want to mirror the same query below to show the same items once again. So two wordpress queries on one page, the first query picking 3 random posts and the second query showing the exact same results of the first query. Any help would be appreciated. Thanks
try this out :)
<?php $my_query = new WP_Query('post_type=post&orderby=rand&showposts=3'); ?>
<?php $i=0; ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
Do Stuff
<?php
$myPostVar[$i] = array (
'title' => get_the_title(),
'content' => get_the_content()
);
$i++;
?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php foreach ($myPostVar as $Postvar) : ?>
<h2><?php echo $Postvar['title']; ?></h2>
<p><?php echo $Postvar['content']; ?></p>
<?php endforeach; ?>
Related
I'm quite new to WordPress Development been following some online tuts and read bits and bobs of documentation but my WP_Query still displays nothing and when I add an else to my loop the else condition displays. I have created the page I'm trying to pull from the database what am I doing wrong here's the code:
<section id="home">
<?php
$query = new WP_Query( array( 'pagename' => 'home' ) );
if ($query->have_post() ) {
while ($query->have_posts() ){
$query->the_post();
echo '<div class="entry-content">';
the_content();
echo '</div>';
}
}
wp_reset_postdata();
?>
</section>
simply change
$query->have_post()
to
$query->have_posts()
use the below code for display content
$args=array(
'post_type' => 'post'
);
$the_query = null;
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content"><?php the_content(); ?></div>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
I am doing theme integration in wordpress.I created home.php (template page) and select from backend(for front end page).
Now i want to show posts on front page.I put following code for display post in front page.But code is not working.Where i am wrong ?
Here is my code
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile;
endif;
?>
You can use following code -
<?php
$args = array('posts_per_page' => 3, 'post__not_in' => get_option('sticky_posts'));
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php esc_html(the_title()); ?>
<?php the_excerpt(); ?>
<?php
endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I am using WordPress 3.8. I want to get query post from a specific category. To do this, I have used the following code
<?php query_posts('post_type=post&category_id=3&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
<?php endif; ?>
I am getting all post instead of a specific category. What is wrong with this code.
category_id=3 should just be cat=3
<?php query_posts('post_type=post&cat=3&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
<?php endif; ?>
In general avoid using query_posts because it is altering the globals inside the main loop.
You can use get_posts():
<?php
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endforeach;
wp_reset_postdata();?>
take in acount that 1 is the id of category (look the id for the category you are interested to fetch on your DB)
Here you will find more info
I've created a custom post type to do hand-crafted excerpts from my blog on my portfolio site. I've got a content window, a link, and a featured image for the post type, which I have called blog.
The issue is that, no matter what I try, the posts are displayed oldest to newest, whereas I would like to display the newest first. Here's the query_posts() call:
<?php query_posts( 'post_type=blog&order=ASC'); ?>
But I've also tried more elaborate queries such as:
<?php query_posts(array('post_type' => 'blog', 'orderby'=>'date','order'=>'ASC')); ?>
My complete template file looks like:
`
">
<div class="sliderContent">
<!--first loop-->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(__('Read More »', THEMENAME)); ?>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.', THEMENAME); ?></p>
<?php endif; ?>
<!--second loop, displays custom post type-->
<?php query_posts(array('post_type' => 'blog', 'orderby'=>'date','order'=>'ASC') ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="contenttile">
<p><?php the_post_thumbnail('medium'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_content(__('Read More »', THEMENAME)); ?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.', THEMENAME); ?></p>
<?php endif; ?>
</div>
</div>
<!-- content end -->
<?php } ?>
`
So I'm displaying the content from the page that this template is applied, then I'm displaying my custom post type.
Thanks for the help, I'm stumped!
Your query is in Ascending order. Ascending order IS oldest to newest. You want DESCENDING order if you want newest to oldest. Also, you should avoid using query_posts if at all possible, as it modifies the default Wordpress loop.
Your second query isn't that much more elaborate than the first. The only difference is you're using an array rather than a string to define the query parameters (which an array is arguably the correct way to go about it), and you're setting the orderby parameter.
Lastly, the default order is by date in descending order (newest to oldest) so you theoretically don't even NEED to define order and orderby parameters.
Try this:
<!--second loop, displays custom post type-->
<?php
$args = array('post_type' => 'blog', 'orderby'=>'date','order'=>'DESC');
/*Consider changing to: $args = array('post_type' => 'blog');*/
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div class="contenttile">
<p><?php the_post_thumbnail('medium'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_content(__('Read More »', THEMENAME)); ?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.', THEMENAME); ?></p>
<?php
endif;
wp_reset_postdata();
?>
</div>
Well, as majorano84 alluded to, after further reading query_posts() is not the right function to use at all (because I guess it makes more work for the server?)
Instead, I used get_posts(), and that displays the posts in the preferred order without any further effort on my part:
<?php
$args = array( 'post_type' => 'blog' );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<div class="contenttile">
<p><?php the_post_thumbnail('medium'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php endforeach; ?>
`
So I'm going to call that the answer, because it solves the problem I was having. Thanks!
s it possible to retrieve post entry for a custom post type by tag, I have been trying with the following code, however it just locks me into a infinte loop.
<aside class="supporting_images">
<?php /*<?php if($post->ID == 241) : echo apply_filters('the_content', '[slideshow=3]'); endif; ?>
<?php the_post_thumbnail(); ?>*/?>
<?php if($post->ID == 241) : ?>
<?php
$query = new WP_Query();
$query->query('tag=branding');
?>
<?php while ($query->have_posts()) : ?>
hello
<?php endwhile; ?>
<?php endif;?>
First try changing your loop to:
<?php while ($query->have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
The have_posts() function just returns true if there is another post but does not increment the loop counter, so the loop will never end.