Wordpress print latest posts - php

I have a static page with 10 containers located at different places in the index.php file, i want to show the 10 latest posts. I want to echo the data for a post (title,text,thumbnail,author,time) like this:
<div class="text">
<?php
echo '$textfrompost3';
?>
</div>
<div class="title">
<?php
echo '$titlefrompost3';
?>
</div>
...
...
My PHP file:
<?php
// the query
$the_query = new WP_Query( array(
'category_name' => 'Allgemein',
'posts_per_page' => 10,
"orderby" => "date",
"order" => "DESC"
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
// insert values into variables
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Thanks in advance, i appreciate any help you can provide.

I'd simply replace your call to WP_Query with get_posts() which will provide an array of post objects.
$latest_posts = get_posts( array(
'category_name' => 'Allgemein',
'posts_per_page' => 10,
) );
I've dropped order and orderby in the example above. While you may well have a different global setting for the number of posts to display, it's unlikely you're modifying the default ordering.
Documentation: https://codex.wordpress.org/Template_Tags/get_posts
So let's say I want to output the title of the third post, I could do the following:
// let's be sure we have a third post.
if ( isset( $latest_posts[2] ) ) {
echo $latest_posts[2]->post_title; // remember arrays have a zero-based index.
}

Related

wordpress php loop - archive is looping the same number of times as results

Not sure that's the best way to to describe it, but here's what's happening (and I'm sure this has to be a reasonably easy thing, just not sure where to look):
Client has 10 offices so I'm using the Advanced Custom Fields plugin with a custom post type to specify a location for job postings. My code seems to be working (i.e. it's pulling the appropriate jobs by location) but it's looping the article tag the same number of times as results. I have 3 jobs in there, so it loops the whole set of results 3 times. If I delete one and drop down to 2, it loops twice, etc. I used the following example from the advanced custom fields website:
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'jobs',
'meta_key' => 'location',
'meta_value' => 'akron'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<h3>Akron Office</h3>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li> <?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I guess I'm not sure where to look (I realize this might be a plugin question rather than a php question) and would appreciate any direction you guys can provide.
You can see the offending page here: http://www.knrlegal.com/jobs/
You should reset the post data, not the query, as stated here:
Class Reference/WP Query, in the "Multiple Loops" section.
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'jobs',
'meta_key' => 'location',
'meta_value' => 'akron'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<h3>Akron Office</h3>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li> <?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset with
* wp_reset_query(). We just need to set the post data back up with
* wp_reset_postdata().
*/
wp_reset_postdata();
?>
<?php endif; ?>

Wordpress Custom Loop doesn't show any posts

I'm trying to add a custom loop to display post titles from the category homepage. Here's the code I have so far so, but it's not displaying any posts. BTW, I'm using this code in single.php.
<?php $recentPosts = new WP_Query(); ?>
<?php if ( $recentPosts->have_posts() ) : ?>
<?php $recentPosts->query_posts( array ( 'category_name' => 'homepage', 'posts_per_page' => 10 ) ); ?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
I'm not too sure what's going on, but would appreciate any help I can get.
You're doing a lot of unnecessary messing around with your query object. Since you're dealing with an archive page, you should be able to create a new template file called category-homepage.php (assuming the slug of the category is homepage). In it, you could place something like the following:
$args = array(
'category_name' => 'homepage',
'posts_per_page' => 10
);
$recentPosts = new WP_Query( $args );
if ( $recentPosts->have_posts() ) :
while ( $recentPosts->have_posts() ) : $recentPosts->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
Again, since it's a category archive page, you shouldn't be doing anything to single.php. You can read more about template hierarchy in the Codex.

Wordpress Query by Tag(Priority) then Category(backfill)

I've search high and low but have been unable to find a solution to my problem. Hopefully this isn't a duplicate question that I was unable to find through search here and on google.
I'm attempting to have a wp_query return a set number of results (10) that are populated by any posts found in the current pages category. I was able to accomplish this with...
$postCategories = get_the_category();
$atts = array (
'posts_per_page' => 10,
'tag' => 'sticky',
'category_name' => $postCategories[0]->slug,
);
But where I'm having trouble is with the tag. I would like any posts that have the tag 'sticky' to take priority over any of the posts being brought in by the category match while not adding any more than 10 results still.
Any help or guidance would be much appreciated as i'm kind of a newbie to php. Thanks
I think this could work for you, but it's hard to know for sure without knowing the specifics of your project.
<ul>
<?php $sticky = get_option( 'sticky_posts' ); // Get sticky posts ?>
<?php $args_sticky = array(
'post__in' => $sticky,
'posts_per_page' => 10, // Limit to 10 posts
'ignore_sticky_posts' => 1
); ?>
<?php $sticky_query = new WP_Query( $args_sticky ); ?>
<?php $sticky_count = count($sticky); // Set variable to the number of sticky posts found ?>
<?php $remaining_posts = 10 - count($sticky); // Determine how many more non-sticky posts you should retrieve ?>
<?php if ($sticky_count > 0) : // If there are any sticky posts display them ?>
<?php while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data ?>
<?php if ($remaining_posts > 0) : // If there are non-sticky posts to be displayed loop through them ?>
<?php $postCategories = get_the_category(); ?>
<?php $loop = new WP_Query( array( 'post_type' => 'post',
'posts_per_page' => $remaining_posts,
'post__not_in' => get_option( 'sticky_posts' ),
'category_name' => $postCategories[0]->slug
) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<?php wp_reset_query(); // Restore global post data ?>
<?php endif; ?>
</ul>

wordpress query_posts alternative

I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!

Wordpress get_posts() returning same post all the time

I am just playing around with WordPress and neither I have any idea abt PHP.
I am trying to fetch few random posts using get_posts() function of the WordPress
my code is something like this
<?php
args1 = array( 'numberposts' => 12 ,'orderby' => 'rand');
$rand_posts1 = get_posts( $args1);
foreach( $rand_posts1 as $randpost1 ) : ?>
<?php the_title(); ?>
<?php endforeach; ?>
But this code is only returning same post all the 12 times and that is the lastest post.
I am clueless what exactly I am doing wrong.
Can any one help me to correct my mistake or point me about my issue.
Try this one
<?php
$args1 = array( 'numberposts' => 12 ,'orderby' => 'rand');
global $post;
//save the current post
$temp=$post;
$rand_posts1 = get_posts( $args1);
foreach( $rand_posts1 as $post ) ://yes this is required, we need $post in setup_postdata
setup_postdata($post); ?>
<?php the_title(); ?>
<?php endforeach;
$post=$temp;//restore current page
?>
That will do it.
Also please take a look at get_posts

Categories