Using WP_Query

February 7, 2018

#PHP #WordPress #WP_Query

WP_Query is typically much safer than using SQL directly in a WordPress environment. But probably less intuitive. I always have to look up what the various arguments actually represent. So this post is to remind me of the basic syntax and structure.

In the first example, from a recent project, we are returning all products which are watches – where client_product is a Custom Post Type. In the second, within a Switch, we are returning a specific selection (from a dropdown).

Using WP_Query with a dropdown selection
Using WP_Query with a dropdown selection

Remember to call wp_reset_postdata() afterwards.

Show Code

Show All Posts