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?
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
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.
How does the get_post syntax work?
- 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.
- By now, you will get a result in the single post where the output is transformed into the object of WP_Post.
- As per the direction of the user, the output will be displayed.
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;
}
?>