How to display latest posts in Wordpress - php

On one of my pages, I want a section to show the latest 3 news posts.
Is there a simple way to retrieve the n latest posts so that they can be displayed?

<?php
function latest_post() {
$args = array(
'posts_per_page' => 3, /* how many post you need to display */
'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post', /* your post type name */
'post_status' => 'publish'
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<?php the_title(); ?>
<?php echo get_the_post_thumbnail('thumbnail'); ?>
/* here add code what you need to display like above title, image and more */
<?php
endwhile;
endif;
}
add_shortcode('lastest-post', 'latest_post');
?>
Add Above code in function.php file.
After that paste below shortcode there you want to display latest post.
Adin side : [lastest-post]
in file : <?php echo do_shortcode('[lastest-post]'); ?>

<?php
//Query 3 recent published post in descending order
$args = array( 'numberposts' => '3', 'order' => 'DESC','post_status' => 'publish' );
$recent_posts = wp_get_recent_posts( $args );
//Now lets do something with these posts
foreach( $recent_posts as $recent )
{
echo 'Post ID: '.$recent["ID"];
echo 'Post URL: '.get_permalink($recent["ID"]);
echo 'Post Title: '.$recent["post_title"];
//Do whatever else you please with this WordPress post
}
?>

Related

how to query only one post in wordpress widget

I am making a wordpress widget for my wordpress theme. In the widget I am trying to query only one post on the front-page.php I am using WP_query for that. But the problem is it is getting all the posts available. I have no idea how to fix this. Any suggestion will be helpful.
My code
public function widget($args, $instance) {
$posts_args = array(
'post_type' => 'post',
'post_per_page' => 1,
'order' => 'DESC'
);
$posts_query = new WP_Query($posts_args);
echo $args['before_widget'];
if($posts_query -> have_posts()):
while($posts_query->have_posts()): $posts_query->the_post();
echo '<div class="widget-single-content" style="background-image: url('.get_the_post_thumbnail_url().')">';
echo '<div class="content-box">';
echo '<h1><span>"</span>'.get_the_title().'<span>"</span></h1>';
echo '<button class="readmore-btn text-captalize">
Read More</button>';
echo '</div>';
echo '</div>';
endwhile;
endif;
echo $args['after_widget'];
//Query
}
$posts_args = array(
'post_type' => 'post',
'numberposts' => 1, //this show how many posts to query from DB
'order' => 'DESC',
'posts_per_page' => 1//this show how many posts display
);
posts_per_page

How can I make this Php WP_Query returning categories only once?

This loop returns the categories of each post, showing it multiple times. Instead, I want to show categories once, this is a filter by category functionality.
<div id="filter-box" class="filter-box">
<?php
$args = array (
'post_type' => "post",
'post_status' => "publish",
'order' => 'ASC',
'orderby' => 'title' ,
'posts_per_page' => -1);
$all_query = new WP_Query($args);
if ($all_query->have_posts()):
while ($all_query->have_posts()):
$all_query->the_post();
?>
<?php get_cat($post->ID); ?>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
This is the function from functions.php
function get_cat($post_id) {
$category_detail=get_the_category($post_id);
foreach($category_detail as $cd) {
echo $cd->cat_name;
}
}
Want to display the categories only once as they appear once for each of the posts.
Please modify you code as:
<?php
$args = array (
'post_type' => "post",
'post_status' => "publish",
'order' => 'ASC',
'orderby' => 'title' ,
'posts_per_page' => -1);
$all_query = new WP_Query($args);
$tempArr = array();
if ($all_query->have_posts()):
while ($all_query->have_posts()):
$all_query->the_post();
if(!in_array($post->ID , $tempArr)){ // check if array has ID not display
array_push($tempArr, $post->ID); //push postID in array
?>
<?php get_cat($post->ID); ?>
<?php
}
endwhile;
endif;
wp_reset_postdata();
?>
Explanation: Please declare an array as $tempArr = array(); and then check if $temArr array has that postID if not then display category and push it in array.

i want to filter category wise post on same page in wordpress

filter category post wise on same page in WordPress if i click category
plastic i want result plastic product
for example product category
1.plastic
2.metallic
3.silver
showing image
enter image description here
post div
<?php
global $post;
$myposts = get_posts(
array(
'post_type' => 'product',
'numberposts' => '999',
'orderby' => 'menu_order',
'order' => 'ASC'
)
);
?>
sidebar div
<?php
global $post;
$curVal = "";
$myposts = get_posts(
array(
'post_type' => 'product',
'numberposts' => '999',
'orderby' => 'product_category',
'order' => 'ASC'
)
);
?>
<ul>
<?php
foreach($myposts as $post){
if($curVal != get_field('franchise_category')) { ?>
<li>
<a href="<?php echo home_url( $wp->request ); ?>?cat=<?php echo get_field('product_category'); ?>">
<?php echo get_field('product_category'); ?>
</a>
</li>
<?php }$curVal = get_field('product_category');} ?>
</ul>
1st if you want to get all posts with this query use:
'numberposts' => -1,// this will return all available, right now you are passing a string 999 - '999'
2nd - you're not setting up your post data and restoring context of the template tags Try this:
foreach ( $myposts as $post ) : setup_postdata( $post );
...
...
endforeach;
wp_reset_postdata();
Edit your code and see the result.

Using a custom Wordpress query to call 5 most recent posts

I am trying to pull the 5 most recent posts of a custom post type using a WP_query. Does the code below look correct? And do I need to use wp_reset_postdata at the end?
<?php
$args = array(
'post_type' => 'webinar_post',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
);
$most_recent = new WP_Query( $args );
?>
<?php if( $most_recent->have_posts() ) ?>
<?php while( $most_recent->have_posts() ) : $most_recent->the_post() ?>
<div class="webinar">
<h2><?php echo get_the_title(); ?> </h2>
<h3><?php echo get_the_date(); ?></h3>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php endif ?>
You don't need to use wp_reset_postdata() unless you are using WP_Query again in the same page. Usage of wp_reset_postdata() is need to set the post data back
Example
<?php
// The 1st Query
$args = [
'post_type' => 'webinar_post',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
];
$most_recent = new WP_Query( $args );
if ( $most_recent->have_posts() ) {
// The Loop
while ( $most_recent->have_posts() ) { $most_recent->the_post();
// your code
}
// Restore original Post Data
wp_reset_postdata();
}
// Updating `$args`
$args['orderby'] = 'post_title'
$args['order'] = 'ASC'
/* The 2nd Query */
$most_recent2 = new WP_Query( $args );
if ( $most_recent2->have_posts() ) {
// The 2nd Loop
while ( $most_recent2->have_posts() ) { $most_recent2->the_post();
// your code
}
// Restore original Post Data
wp_reset_postdata();
}
?>

Wordpress loop inside loop

I have a normal loop that outputs posts based on the given $args.
After three posts I want to insert a post that is from a Featured category. I've tried starting a new WP_Query, simple query_posts in different combinations. Nothing seems to work. Any ideas why ?
$args = array(
'post_type' => 'post',
'posts_per_page' => $count,
'paged' => $paged,
'page' => $paged,
'cat' => $cat,
'ignore_sticky_posts' => 1
);
// create a new instance of WP_Query
$my_query = new WP_Query($args);
<ul>
<?php
$i = 0;
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
if($i == 3): ?>
<li> //insert here one post from featured category
</li>
<?php endif; ?>
<li>
// the normal query stuff is here
</li>
<?php $i++ //post counter
endwhile; //end loop while
endif; //end loop
?>
</ul>
I did it this way. Before the main query I did a special query for the featured post.
$args2 = array(
'post_type' => 'post',
'posts_per_page' => 1,
'category_name' => 'TheCategoryName-Slug',
'ignore_sticky_posts' => 1
);
$my_query2 = new WP_Query($args2);
$post_ids = array(); //create an array to store the ids of the posts
if ($my_query2->have_posts()) : while ($my_query2->have_posts()) : $my_query2->the_post();
$post_ids[] = get_the_ID();
endwhile;
wp_reset_postdata();
endif;
After you have all the ids you can use them to get the data you need. Basically, all the data from the wp_posts table (http://codex.wordpress.org/Database_Description#Table:_wp_posts). The post author is returned as an id that can be used to get the name using get_userdata() function.
echo get_post_field('post_title', $post_ids[0]);
echo get_post_field('post_author', $post_ids[0]);
echo get_post_field('post_date', $post_ids[0]);

Categories