{"id":88818,"date":"2023-07-14T18:25:14","date_gmt":"2023-07-14T18:25:14","guid":{"rendered":"\/tutorials\/?p=88818"},"modified":"2026-03-10T09:28:16","modified_gmt":"2026-03-10T09:28:16","slug":"get_posts-function-wordpress","status":"publish","type":"post","link":"\/ca\/tutorials\/get_posts-function-wordpress","title":{"rendered":"What Is the WordPress get_posts Function and How to Use It to Build PHP Post Lists"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p>The WordPress <strong>get_posts <\/strong>function lets you retrieve posts, pages, and custom post types based on given parameters. It works by filtering the posts based on criteria like date, author, type, permission, or meta key.<\/p><p>WordPress site owners can use this function to show relevant content to users quickly. For example, they can display lists of posts with the most comments, the same author, or a similar category.<\/p><p>In this article, we will explain the WordPress <strong>get_posts <\/strong>function, including its parameters and objects. You will also learn how to use it to create custom queries for various tasks.<\/p><p class=\"has-text-align-center\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Mega-WordPress-Cheat-EN.pdf\" target=\"_blank\" rel=\"noopener\">Download all-in-one WordPress cheat sheet<\/a><\/p><p>\n\n\n\n<div class=\"protip\">\n                    <h2 class=\"featured-snippet title\">What Is the WordPress get_posts Function?<\/h2>\n                    <p><br>\nThe WordPress <strong>get_posts<\/strong> function is a feature that lets you query posts based on determined parameters, like the publication date, author, type, permission, or meta key.<br>\n<br>\nWordPress developers use it to quickly find posts matching their given criteria. For example, they can show lists of posts with the most views, the same publication date, or a similar category.<\/p>\n                <\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-how-does-wordpress-get-posts-work\">How Does WordPress get_posts Work?<\/h3><p>The WordPress <strong>get_posts <\/strong>function turns PHP code into SQL queries to fetch post data from your database. These queries are based on your given parameters &ndash; the criteria to filter the posts.<\/p><p>To construct the SQL queries, this WordPress function uses the <strong>WP_query <\/strong>class. After processing the queries, the database will return an array of<strong> WP_post <\/strong>results, each representing an individual post object.<\/p><p><div><p class=\"important\"><strong>Important!<\/strong> In this case, <strong>posts<\/strong> refer to WordPress posts, pages, and custom post types.<\/p><\/div>\n\n\n\n<\/p><p><strong>WP_query<\/strong> can also pull post content from the WordPress database and display it on the web browser. However, it will alter the <a href=\"\/ca\/tutorials\/the-loop-wordpress\">WordPress loop<\/a>, causing issues if you don&rsquo;t reset it after executing a query.<\/p><p>Meanwhile, the <strong>get_posts <\/strong>function automatically resets the loop whenever you run a WordPress query. It lets you send multiple post queries to the WordPress database without issues.<\/p><p><div class=\"protip\">\n                    <h4 class=\"title\">Suggested Reading<\/h4>\n                    <p>Check out our other WordPress tutorials:<br>\n<a href=\"\/ca\/tutorials\/wordpress-export-posts\">How to Export Posts on WordPress<\/a><br>\n<a href=\"\/ca\/tutorials\/how-to-add-wordpress-related-posts\">How to Add WordPress Related Posts<\/a><br>\n<a href=\"\/ca\/tutorials\/wordpress\/how-to-schedule-posts-in-wordpress\">How to Schedule Posts in WordPress<\/a><\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-wordpress-get-posts-function-parameters\">WordPress get_posts Function Parameters<\/h2><p>The <strong>get_posts <\/strong>function has various parameters to apply custom filters. Some of the most used parameters in the WordPress core include:<\/p><ul class=\"wp-block-list\">\n<li><strong>Numberposts <\/strong>&ndash; sets the number of posts the function will retrieve. The default value is five.<\/li>\n\n\n\n<li><strong>Category <\/strong>&ndash; determines the retrieved posts&rsquo; category.<\/li>\n\n\n\n<li><strong>Include <\/strong>&ndash; defines the post IDs you want to include in the query results.<\/li>\n\n\n\n<li><strong>Exclude <\/strong>&ndash; specifies the post IDs you want to exclude from the query results.<\/li>\n\n\n\n<li><strong>Post_type <\/strong>&ndash; filters the post object type your custom query will retrieve. It can be posts, pages, or a custom post type.<\/li>\n\n\n\n<li><strong>Post_status <\/strong>&ndash; affects whether your query will return draft, private, or published posts.<\/li>\n\n\n\n<li><strong>Order <\/strong>&ndash; sorts the retrieved posts in ascending or descending order.<\/li>\n\n\n\n<li><strong>Orderby <\/strong>&ndash; sets the sorting criteria, such as the post date or the number of comments. Use <strong>rand <\/strong>to retrieve random posts.<\/li>\n\n\n\n<li><strong>Post_mime_type <\/strong>&ndash; queries attachment post types based on mime type parameters.<\/li>\n\n\n\n<li><strong>Suppress_filters<\/strong> &ndash; determines whether to suppress filters so the query cannot be altered. This parameter defaults to <strong>TRUE<\/strong>.<\/li>\n<\/ul><p>Without custom parameters, the <strong>get_posts <\/strong>function will retrieve the latest posts by default. You must attach an array of <strong>object<\/strong> posts to the function and specify the arguments for filtering the posts using this syntax:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$example_array = get_posts($arguments);\n&nbsp;&nbsp;&nbsp;$arguments = array(\n&nbsp; &nbsp; &nbsp; \"parameter_1\" =&gt; value,&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"parameter_2\" =&gt; value\n&nbsp;&nbsp;&nbsp;);<\/pre><h3 class=\"wp-block-heading\" id=\"h-wp-posts-objects\">WP_Posts Objects<\/h3><p>The WordPress <strong>get_posts <\/strong>function returns an array of <strong>post <\/strong>objects containing various information about your website posts. Some of the common properties of the <strong>post <\/strong>objects include:<\/p><ul class=\"wp-block-list\">\n<li><strong>ID<\/strong> &ndash; the post ID number.<\/li>\n\n\n\n<li><strong>post_author <\/strong>&ndash; the post author&rsquo;s numeric user ID.<\/li>\n\n\n\n<li><strong>post_title<\/strong> &ndash; the post title.<\/li>\n\n\n\n<li><strong>post_date<\/strong> &ndash; the publication date of the post in YYYY-MM-DD format.<\/li>\n\n\n\n<li><strong>post_content<\/strong> &ndash; the content of the post.<\/li>\n\n\n\n<li><strong>post _status<\/strong> &ndash; the current status of the retrieved post, like published or draft.<\/li>\n\n\n\n<li><strong>post_modified<\/strong> &ndash; the post&rsquo;s last modification date.<\/li>\n\n\n\n<li><strong>comment_count <\/strong>&ndash; the number of comments on the post.<\/li>\n<\/ul><p>Use <strong>foreach <\/strong>to loop the post arrays and <strong>echo <\/strong>to display them. Here&rsquo;s an example of the code structure:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">foreach($example_array as $post)\n&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"&lt;h1&gt;\" . $post-&gt;post_title . \"&lt;\/h1&gt;&lt;br&gt;\";\n&nbsp;&nbsp;&nbsp;}<\/pre><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/The-File-Manager-button-in-hPanel-website-management-menu.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1460\" height=\"531\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/07\/The-File-Manager-button-in-hPanel-website-management-menu.png\" alt=\"The File Manager button in hPanel website management menu\" class=\"wp-image-88820\"  sizes=\"auto, (max-width: 1460px) 100vw, 1460px\" \/><\/a><\/figure><\/div><p>To show the results on any page, add the <strong>get_posts <\/strong>function to a <a href=\"\/ca\/tutorials\/how-to-create-wordpress-child-theme\">child theme<\/a> template. Alternatively, include it in the <strong>blog.php<\/strong> file to show the results on a blog page. On <a href=\"\/ca\/wordpress-hosting\">Hostinger&rsquo;s WordPress plans<\/a>, these files are accessible via the <strong>File Manager<\/strong>.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-wordpress-hosting\" href=\"\/ca\/wordpress-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner-1024x300.png\" alt=\"\" class=\"wp-image-111781\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-how-to-use-the-get-posts-function-in-wordpress\">How to Use the get_posts Function in WordPress<\/h2><p>Due to its various parameters, the WordPress <strong>get_posts <\/strong>function has diverse use cases. In this section, we will explain the most common ones.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-get-the-most-popular-posts\">How to Get the Most Popular Posts<\/h3><p>Some WordPress sites have widgets showing the most popular posts. To enable it, use <strong>get_posts <\/strong>to query an array of post IDs based on the number of comments:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n&nbsp;&nbsp;&nbsp;$arguments = array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"orderby\" =&gt; \"comment_count\",&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"posts_per_page\" =&gt; 10\n&nbsp;&nbsp;&nbsp;);\n&nbsp;&nbsp;&nbsp;$posts_array = get_posts($arguments);\n&nbsp;&nbsp;&nbsp;foreach($posts_array as $post)\n&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"&lt;h1&gt;\" . $post-&gt;post_title . \"&lt;\/h1&gt;&lt;br&gt;\";\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"&lt;p&gt;\" . $post-&gt;post_ID . \"&lt;\/p&gt;&lt;br&gt;\";\n&nbsp;&nbsp;&nbsp;}&nbsp;\n?&gt;<\/pre><p>In the example, we specify arguments that contain an array of parameters that will filter the queried posts. In this case, we use the <strong>orderby <\/strong>parameter to sort the posts based on the number of comments.<\/p><p>We also use <strong>post_per_page <\/strong>to limit the value of displayed posts per page to <strong>10<\/strong>. Then, we create an array that calls for the <strong>get_posts <\/strong>function, which uses <strong>$arguments <\/strong>to filter the posts.<\/p><p>After the function retrieves the posts, the <strong>foreach <\/strong>method loops the results. Then, the <strong>echo <\/strong>function outputs the retrieved post ID and heading.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-get-posts-by-author\">How to Get Posts by Author<\/h3><p>If your WordPress website has multiple contributors, manually finding posts from a specific author can be time-consuming. To quickly do so, use the <strong>author <\/strong>parameter to filter the posts:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n&nbsp;&nbsp;&nbsp;$arguments = array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"numberposts\" =&gt; -1,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"author\" =&gt; 2\n&nbsp;&nbsp;&nbsp;);\n&nbsp;&nbsp;&nbsp;$sample_array = get_posts($arguments);\n&nbsp;&nbsp;&nbsp;foreach($sample_array as $post)\n&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"&lt;h3&gt; \" . $post-&gt;post_title . \"&lt;\/h3&gt;\";\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"&lt;p&gt;\" . $post-&gt;post_date . \"&lt;\/p&gt;\";\n&nbsp;&nbsp;&nbsp;}\n?&gt;<\/pre><p>In the arguments, we use two parameters &ndash; <strong>numberposts <\/strong>and <strong>author<\/strong>. The <strong>numberposts <\/strong>parameter will limit the number of posts shown. Since we set it to <strong>-1<\/strong>, all posts will be retrieved.<\/p><p>Meanwhile, the <strong>author <\/strong>parameter will filter the posts and only show those from a specific author based on their ID &ndash; in this case, the example author&rsquo;s ID is <strong>2<\/strong>. To query multiple authors, specify the IDs in a comma-separated list.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-get-posts-in-the-same-category\">How to Get Posts in the Same Category<\/h3><p>The WordPress <strong>get_posts<\/strong> function is commonly used to retrieve posts based on their categories. There are different parameters for this task, most commonly:<\/p><ul class=\"wp-block-list\">\n<li><strong>category<\/strong> &ndash; based on the category ID.<\/li>\n\n\n\n<li><strong>category_name<\/strong> &ndash; using the category slug, like <strong>news<\/strong> or<strong> sponsored<\/strong>.<\/li>\n\n\n\n<li><strong>category__and<\/strong> &ndash; querying an array of multiple category IDs.<\/li>\n\n\n\n<li><strong>category_not_in <\/strong>&ndash; getting posts from an excluded category ID.<\/li>\n<\/ul><p>Regardless of the parameter, the syntax is the same. Here&rsquo;s an example of a code snippet for querying posts from a category with the ID of <strong>1<\/strong>:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n&nbsp;&nbsp;&nbsp;$arguments = array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"numberposts\" =&gt; -1,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"category\" =&gt; 1,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"orderby\" =&gt; \"date\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"order\" =&gt; \"DESC\"\n&nbsp;&nbsp;&nbsp;);\n&nbsp;&nbsp;&nbsp;$category_array = get_posts($arguments);\n&nbsp;&nbsp;&nbsp;foreach($sample_array as $post)\n&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"&lt;h3&gt; \" . $post-&gt;post_title . \"&lt;\/h3&gt;\";\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"&lt;p&gt;\" . $post-&gt;post_ID . \"&lt;\/p&gt;\";\n&nbsp;&nbsp;&nbsp;}\n?&gt;<\/pre><p>In the example, we set the number of posts to <strong>-1<\/strong> to show all of them and specify the category ID as the filter. We also use the <strong>orderby<\/strong> and <strong>order <\/strong>parameters to sort the queried posts in descending order based on their date.<\/p><p>The <strong>get_posts<\/strong> function will return posts into <strong>$category_array<\/strong> based on the <strong>$arguments <\/strong>parameters. Then, the code loops the results to display the queried post ID and title.<\/p><p>If you want to retrieve posts with multiple categories, replace the<strong> category <\/strong>parameter with <strong>category__and<\/strong>. Then, use a comma-separated list of category IDs as the value. For example, here&rsquo;s a code that will show posts with 1, 2, and 3 categories:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\"category__and\" =&gt; array(\"1\",\"2\",\"3\")<\/pre><p>The <strong>category<\/strong> and <strong>category_name <\/strong>parameters can also take an array as their value. Unlike <strong>category__and<\/strong>, they will retrieve posts from any of the specified categories. Consider the following example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\"category_name\" =&gt; array(\"news\",\"archive\",\"sponsored\")<\/pre><p>The code example will show all posts that belong to one or more of the specified categories. If you use <strong>category__and<\/strong>, the function only shows posts from all three categories.<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-get-posts-with-matching-meta-keys-and-value\">How to Get Posts With Matching Meta Keys and Value<\/h3><p>If your WordPress posts have a custom field, you can use <strong>get_posts<\/strong> to retrieve them based on a matching meta key and value. Here are the important parameters and their functions:<\/p><ul class=\"wp-block-list\">\n<li><strong>meta_key<\/strong> &ndash; fetching posts based on the custom field key.<\/li>\n\n\n\n<li><strong>meta_value<\/strong> &ndash; querying posts based on the meta key&rsquo;s string value.<\/li>\n\n\n\n<li><strong>meta_value_num<\/strong> &ndash; filtering posts similar to <strong>meta_value<\/strong> but uses a numerical value.<\/li>\n\n\n\n<li><strong>meta_compare<\/strong> &ndash; applying an operation to test the meta value parameter.<\/li>\n<\/ul><p>Users can use these parameters together to retrieve posts with a specific meta key and value. Consider the following example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n&nbsp;&nbsp;&nbsp;$arguments = array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"numberposts\" =&gt; -1,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"meta_key\" =&gt; \"color\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"meta_value\" =&gt; \"blue\",&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"orderby\" =&gt; \"date\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"order\" =&gt; \"ASC\"\n&nbsp;&nbsp;&nbsp;);\n&nbsp;&nbsp;&nbsp;$category_array = get_posts($arguments);\n&nbsp;&nbsp;&nbsp;foreach($sample_array as $post)\n&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"&lt;h3&gt; \" . $post-&gt;post_title . \"&lt;\/h3&gt;\";\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"&lt;p&gt;\" . $post-&gt;post_date . \"&lt;\/p&gt;\";\n&nbsp;&nbsp;&nbsp;}\n?&gt;<\/pre><p>The code queries all posts that have the <strong>color<\/strong> custom field with a <strong>blue<\/strong> value. If you omit the <strong>meta_key <\/strong>parameter, it will retrieve all posts with a<strong> blue<\/strong> value, regardless of their custom field name. Conversely, removing <strong>meta_value<\/strong> will display posts with the <strong>color <\/strong>custom field with any value.<\/p><p>Meanwhile, <strong>meta_compare <\/strong>lets you specify an operation to modify how the meta key and value operate. Consider the following code snippet:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n&nbsp;&nbsp;&nbsp;$arguments = array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"numberposts\" =&gt; -1,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"meta_key\" =&gt; \"color\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"meta_value\" =&gt; \"blue\",&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"meta_compare\" =&gt; \"!=\",&nbsp;\n&nbsp;&nbsp;&nbsp;);\n?&gt;<\/pre><p>We use the<strong> !=<\/strong> value to exclude posts with the specified meta key value. So, it will show an array of posts excluding ones with the <strong>blue <\/strong>meta value. We recommend referring to the <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_meta_query\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress documentation<\/a> for the full operation values.<\/p><p>You may also use multiple meta key-and-value pairs to create a more specific query. To do so, use the <strong>meta_query <\/strong>parameter with the filter arrays as its value. Then, use the <strong>relation<\/strong> function with the <strong>AND<\/strong> or <strong>OR<\/strong> value to establish relationships between the arrays. Here&rsquo;s the syntax:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n&nbsp;&nbsp;&nbsp;$arguments = array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"post_type\" =&gt; \"postname\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"meta_query\" =&gt; array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"relation\" =&gt; \"relation\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"key\" =&gt; \"keyname\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"value\" =&gt; \"keyvalue\",&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"compare\" =&gt; \"operation\",&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"key\" =&gt; \"keyname\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"value\" =&gt; \"keyvalue\",&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"compare\" =&gt; \"operation\",&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),\n&nbsp;&nbsp;&nbsp;);\n?&gt;<\/pre><h3 class=\"wp-block-heading\" id=\"h-how-to-get-custom-post-type-with-custom-taxonomy\">How to Get Custom Post Type With Custom Taxonomy<\/h3><p><a href=\"\/ca\/tutorials\/wordpress-taxonomy\/\">WordPress taxonomy<\/a> is a mechanism that lets you group content based on its relationships or characteristics. The default taxonomies for WordPress posts are category and tag, but you can add as many new ones as you need.<\/p><p>If your WordPress site has a custom post type, taxonomy helps categorize its content more specifically. For example, a movie post type may have custom taxonomies such as genre and release date.<\/p><p>Querying posts from the same taxonomy uses a similar function to category. Use the custom taxonomy name as the parameter and specify the term name or identifier as the value. For example, the code snippet below will query a custom post type named <strong>movie<\/strong> for posts with the <strong>review <\/strong>genre:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n&nbsp;&nbsp;&nbsp;$arguments = array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"numberposts\" =&gt; -1,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"post_type\" =&gt; \"movie\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"genre\" =&gt; \"review\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"orderby\" =&gt; \"date\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"order\" =&gt; \"ASC\"\n&nbsp;&nbsp;&nbsp;);\n&nbsp;&nbsp;&nbsp;$category_array = get_posts($arguments);\n&nbsp;&nbsp;&nbsp;foreach($sample_array as $post)\n&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"&lt;h3&gt; \" . $post-&gt;post_title . \"&lt;\/h3&gt;\";\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo \"&lt;p&gt;\" . $post-&gt;post_date . \"&lt;\/p&gt;\";\n&nbsp;&nbsp;&nbsp;}\n?&gt;<\/pre><p>You can also use more than one parameter to create a complex post query using <strong>tax_query<\/strong>. It takes an array of arrays as the value, with each containing the following parameters:<\/p><ul class=\"wp-block-list\">\n<li><strong>taxonomy<\/strong> &ndash; the taxonomy name.<\/li>\n\n\n\n<li><strong>field<\/strong> &ndash; the method for selecting the taxonomy term, whether using an ID, slug, or name. It uses the ID by default.<\/li>\n\n\n\n<li><strong>terms<\/strong> &ndash; the taxonomy term based on the field. To filter multiple taxonomies, use an array as the value.<\/li>\n\n\n\n<li><strong>include_children<\/strong> &ndash; the rule defining whether to include the taxonomy child. It is set to <strong>TRUE <\/strong>by default.<\/li>\n\n\n\n<li><strong>operator<\/strong> &ndash; the logical operation for the parameters. Values include <strong>IN<\/strong>, <strong>NOT<\/strong> <strong>IN<\/strong>, <strong>AND<\/strong>, <strong>EXISTS<\/strong>, and <strong>NOT EXISTS<\/strong>.<\/li>\n<\/ul><p>For example, the following code will query posts with the <strong>John<\/strong> tag from the <strong>People<\/strong> taxonomy:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n&nbsp;&nbsp;&nbsp;$arguments = array(\n&nbsp;&nbsp;&nbsp;\"numberposts\" =&gt; -1,\n&nbsp;&nbsp;&nbsp;\"tax_query\" =&gt; array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"taxonomy\" =&gt; \"people\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"field\" =&gt; \"slug\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"terms\" =&gt; \"john\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),\n&nbsp;&nbsp;&nbsp;),\n&nbsp;&nbsp;&nbsp;\"orderby\" =&gt; \"date\",\n&nbsp;&nbsp;&nbsp;\"order\" =&gt; \"ASC\"\n&nbsp;&nbsp;&nbsp;);\n?&gt;<\/pre><p>To use multiple taxonomy queries, add another array into the<strong> tax_query<\/strong> parameter. Also, specify the relation between the arrays using the <strong>relation <\/strong>parameters with <strong>AND<\/strong> or <strong>OR <\/strong>values. Here&rsquo;s an array example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n&nbsp;&nbsp;&nbsp;$arguments = array(\n&nbsp;&nbsp;&nbsp;\"tax_query\" =&gt; array(\n&nbsp;&nbsp;&nbsp;\"relation\" =&gt; \"AND\",\n&nbsp;&nbsp;&nbsp;array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"taxonomy\" =&gt; \"movie_genre\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"field\" =&gt; \"slug\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"terms\" =&gt; array( \"thriller\", \"fantasy\" ),\n&nbsp;&nbsp;&nbsp;),\n&nbsp;&nbsp;&nbsp;array(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"taxonomy\" =&gt; \"actor\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"field\" =&gt; \"term_id\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"terms\" =&gt; array( 90, 105, 207 ),\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"operator\" =&gt; \"NOT IN\",\n&nbsp;&nbsp;&nbsp;),\n&nbsp;&nbsp;&nbsp;),\n?&gt;<\/pre><p><div class=\"protip\">\n                    <h4 class=\"title\">Suggested Reading<\/h4>\n                    <p>Learn more about WordPress PHP functions:<br>\n<a href=\"\/ca\/tutorials\/wordpress-add-filter\">How to Use WordPress add_filter<\/a><br>\n<a href=\"\/ca\/tutorials\/how-to-use-wp_update_post\">How to Use WordPress wp_update_post<\/a><br>\n<a href=\"\/ca\/tutorials\/wordpress-get-post-meta\">How to Use WordPress get_post_meta Function<\/a><\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>The WordPress <strong>get_posts<\/strong> function is a method to retrieve an array of posts, pages, or custom post types from the database based on specific query parameters. It is used to quickly display posts matching your given criteria, like comment count or meta keys.<\/p><p>This function uses the<strong> WP_query <\/strong>class to construct an SQL query and send it to the WordPress database. The database then returns an array of <strong>post<\/strong> objects representing the website posts.<\/p><p>There are various parameters of <strong>get_posts<\/strong>, such as <strong>numberposts<\/strong>, <strong>category<\/strong>, and <strong>post_type<\/strong>. It also returns <strong>WP_posts<\/strong> objects containing post data like <strong>ID<\/strong>, <strong>post_author<\/strong>, and <strong>post_title<\/strong>.<\/p><p>Developers use the WordPress <strong>get_posts<\/strong> function for various tasks and scenarios. Commonly, you can utilize it to retrieve posts with the most comments, the same author, similar categories, and custom taxonomies.<\/p><h2 class=\"wp-block-heading\" id=\"h-wordpress-get-posts-faq\">WordPress get_posts FAQ<\/h2><p>To help you understand more about the WordPress <strong>get_posts <\/strong>function, we will answer several commonly asked questions about it.<\/p><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1688729800263\"><h3 class=\"schema-faq-question\">What&rsquo;s the Difference Between get_posts and WP_query?<\/h3> <p class=\"schema-faq-answer\">Both are methods for querying and displaying posts from your website&rsquo;s database. However, the <strong>WP_query <\/strong>class alters the WordPress loop after each query, potentially causing errors if used improperly.<br><br>Meanwhile, the <strong>get_posts <\/strong>method automatically resets the loop after displaying the requested posts, making it less prone to errors and more convenient.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1688729819130\"><h3 class=\"schema-faq-question\">What Is the Difference Between the get_posts and get_pages Functions?<\/h3> <p class=\"schema-faq-answer\">Unlike<strong> get_posts<\/strong>, the <strong>get_pages<\/strong> function doesn&rsquo;t use the <strong>WP_query <\/strong>class and queries the database directly. These WordPress functions also have different parameters and retrieved information.For example, <strong>get_pages<\/strong> doesn&rsquo;t use the meta value and key parameters. It also only queries pages and hierarchical post types.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>The WordPress get_posts function lets you retrieve posts, pages, and custom post types based on given parameters. It works by filtering the posts based on criteria like date, author, type, permission, or meta key. WordPress site owners can use this function to show relevant content to users quickly. For example, they can display lists of [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/ca\/tutorials\/get_posts-function-wordpress\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":337,"featured_media":139393,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"What Is the WordPress get_posts Function and How to Use It","rank_math_description":"WordPress get_posts is a function to retrieve a custom set of posts based on specified criteria in a PHP list. Check this article to learn more.","rank_math_focus_keyword":"wordpress get_posts","footnotes":""},"categories":[22698,22692],"tags":[],"class_list":["post-88818","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-advanced","category-wordpress"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/get_posts-function-wordpress","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/get_posts","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/get_posts-funcion-wordpress","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/get_posts-function-wordpress","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/wordpress-get-posts-function","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/wordpress-get-posts-function","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/get_posts-funcion-wordpress","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/get_posts-funcion-wordpress","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/get_posts-funcion-wordpress","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/get_posts","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/get_posts-function-wordpress","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/get_posts-function-wordpress","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/get_posts-function-wordpress","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/get_posts-function-wordpress","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/88818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/users\/337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/comments?post=88818"}],"version-history":[{"count":13,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/88818\/revisions"}],"predecessor-version":[{"id":137816,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/88818\/revisions\/137816"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/media\/139393"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/media?parent=88818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/categories?post=88818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/tags?post=88818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}