Do you the syntax get_posts used in WordPress?

Do you the syntax get_posts used in WordPress?

You may have a greater number of post and pages in your WordPress account. Do you have enough time to sit and search relaxedly the post you want? Not sure right? To make it simple, we have used a syntax called get_post which helps to get the post you want within a few seconds.

How does the get_post syntax work?
  1. As per the criteria of the user search and the object WP_Query, the get_post helps to transform the code of PHP into the query of SQL directly to the WordPress DB website.
  2. By now, you will get a result in the single post where the output is transformed into the object of WP_Post.
  3. As per the direction of the user, the output will be displayed.
Here is a big play of Loop. The syntax WP_Query creates the loop and so, it goes for a deep search in the database to find out the post. The bigger concept of the loop gets inside the page and posts and found the correct one which you want to see.

To call the function, you can use the syntax get_posts();

In case of assigning a variable, kindly use $ sample_array = get_posts();

There are some parameters used in the syntax such as
  • Category: Depending upon the ID, the output should be conveyed in terms of group or not
  • Post_type: After using get_post, it chooses the page, post, etc.
  • Orderby: Output parameter finding
  • Include: Shows the array of output
  • Exclude: If you want to not display anything use this query
  • Order: Arrangement of the output
  • Post_status: Whether the post is running, deleted, or scheduled.
Code:
<?php
$count = 1;
# Custom parameters added inside arguments array
$arguments = array(
"numberposts" => 10
);
# Arguments variable provided as argument to get_posts function
$sample_array = get_posts($arguments);
foreach($sample_array as $post)
{
echo "<h3> ${count}. " . $post->post_title . "</h3>";
++$count;
}
?>
5 is the default parameter of numberposts, simultaneously we have increased 5 to 10. Like this, you can prefer the parameters and use them to make your work easy.
Author
kumkumsharma
Views
1,267
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top