In this part of this series on page_id in our example. This 20 is cast to an integer because post_status argument to post__in argument to fetch posts, WordPress will still fetch sticky posts, even if they're not in your list, as you can see in the image above. To omit them, you use the post__in and post_type parameter in this code to just query for pages.
Querying for Post Types
In some of the examples above, I've used the post_type is usually set to any whenever you use the tax_query<code>tax_query
argument in your queries.
To give a simple example, here's how you'd query for all of your site's pages:
$args = array(<br> 'post_type' => 'page'<br>);<br>
Custom Post Types
Querying for a custom post type is simple: use the name you gave the post type when registering it, not the title that's used in the admin menus. So let's say you registered your product post types using register_post_type()<code>register_post_type()
as follows:
function register_product() {<br><br> $args = array(<br> 'name' => __( 'Products', 'tutsplus' ),<br> 'singular_name' => __( 'Product', 'tutsplus' )<br> );<br><br> register_post_type( 'product', $args );<br>}<br>
The value you use for the post_type<code>post_type
argument when querying for products isn't 'Product'<code>'Product'
or 'Products'<code>'Products'
but 'product'<code>'product'
:
$args = array(<br> 'post_type' => 'product'<br>);<br>
Attachments
By default, if you try to run a query for attachments, it won't work. That's because WordPress sets the post_status<code>post_status
of attachments to inherit<code>inherit
and WP_Query<code>WP_Query
defaults to 'post_status' => 'publish'<code>'post_status' => 'publish'
unless you specify otherwise. So if you want to query for attachments, you must include the post_status<code>post_status
argument:
$args = array(<br> 'post_type' => 'attachment',<br> 'post_status' => 'inherit'<br>);<br>
Note that you could also use any<code>any
instead of inherit<code>inherit
.
Summary
Using WP_Query<code>WP_Query
to create custom queries for posts and post types is something I do a lot. As you've seen from the examples here, there are plenty of possibilities:
- Use it to query for top-level pages in your site.
- Use it to query for posts of a specific post type.
- Use it to query for all posts except the ones you specify.
- Use it to query for all the children of the current page.
There are many more possibilities using the arguments covered here, but this should give you a taster.
This post has been updated with contributions from Nitish Kumar. Nitish is a web developer with experience in creating eCommerce websites on various platforms. He spends his free time working on personal projects that make his everyday life easier or taking long evening walks with friends.
The above is the detailed content of WP_Query Arguments: Posts, Pages, and Post Types. For more information, please follow other related articles on the PHP Chinese website!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor
