WP_Query – WordPress Snippet Generator

Use this WordPress code snippet generator to build complex argument arrays for the WP_Query class.

  • General
  • Post/Page
  • Pagination
  • Author
  • Password
  • Misc.
  • Cache

General

Item
Input

Posts and Pages

Item
Input

Pagination Parameters

Item
Input

Author

Item
Input

Password

Item
Input

Search

Item
Input

Order

Item
Input










Custom Field Parameters

Item
Input















Permission Parameters

Item
Input

Caching Parameters

Item
Input

Register Custom Status

Add this code to your functions.php file. Scroll down to learn more.

      
// WP_Query arguments
${{themeSlug || 'your_theme_slug'}}_args = array( 'p' => '{{postId}}','name' => '{{postSlug}}','title' => '{{postTitle}}','post_parent' => '{{postParent}}','page_id' => '{{pageId}}','pagename' => '{{pageName}}','post_type' => '{{postType}}','post_status' => '{{postStatus}}','has_password' => true,'post_password' => '{{postPassword}}','author' => '{{authorId}}','author_name' => '{{authorName}}','order' => 'DESC','order' => 'ASC','orderby' => 'ID','orderby'   => 'author','orderby' => 'title','orderby' => 'name','orderby' => 'date','orderby' => 'modified','orderby'   => 'parent','orderby' => 'rand','orderby' => 'comment_count','orderby' => 'menu_order','meta_compare' => '=','meta_compare'   => '!=','meta_compare' => '>','meta_compare' => '>=','meta_compare' => '<','meta_compare' => '<=','meta_compare' => 'LIKE','meta_compare' => 'NOT LIKE','meta_compare' => 'IN','meta_compare' => 'NOT IN','meta_compare' => 'BETWEEN','meta_compare' => 'NOT BETWEEN','meta_compare' => 'NOT EXISTS','meta_compare' => 'REGEXP','meta_compare' => 'NOT REGEXP','meta_compare' => 'RLIKE','s' => '{{searchString}}','perm' => '{{userPermission}}','cache_results' => true,'update_post_meta_cache' => true,'update_post_term_cache' => true,'nopaging' => true,'posts_per_page' => '{{postsPerPage}}','posts_per_archive_page' => '{{postsPerArchivePage}}','offset' => '{{offset}}','ignore_sticky_posts' => true,'paged' => '{{paged}}','page' => '{{page}}','meta_key' => '{{metaKey}}','meta_value' => '{{metaValue}}','meta_value_num' => '{{metaValueNum}}',);

// The Query
${{themeSlug || 'your_theme_slug'}}_{{queryVariable || 'your_query_variable'}} = new WP_Query( ${{themeSlug || 'your_theme_slug'}}_args );

// The Loop
if ( ${{themeSlug || 'your_theme_slug'}}_{{queryVariable || 'your_query_variable'}}->have_posts() ) {
	while ( ${{themeSlug || 'your_theme_slug'}}_{{queryVariable || 'your_query_variable'}}->have_posts() ) {
		${{themeSlug || 'your_theme_slug'}}_{{queryVariable || 'your_query_variable'}}->the_post();
		// Do layout
	}
} else {
	// No posts
}

// Reset Post Data
wp_reset_postdata();

    

Use this handy code snippet tool to create custom WordPress queries to display unique content. Read on to learn about the snippet generator and how to use it.

What Is WP_Query and What Can I Do With It?

In the back-end, WordPress runs queries on each page that your site displays. The query it runs depends on the type of page that the user has requested. For instance, if the user is viewing an archive page, the query’s job is to retrieve all posts from that archive.

What if you want to create custom queries to display very specific content that wouldn’t normally be retrieved by WordPress’ standard queries? The WP_Query class allows developers to create custom queries using a number of parameters to define exactly what content you want the query to retrieve and how to output it.

The WP_Query class contains four main elements:

  1. Arguments for the query.
  2. The query itself.
  3. The loop.
  4. Closing if and while tags.

How Do I Use This WP_Query Snippet Generator Tool?

We’ve put together this WP_Query snippet generator tool to help developers create custom queries for their themes without having to hand-code them. By using this tool, all that’s left to do is set values for the required parameters based on your preference and the tool will generate a theme-specific, custom code snippet for you.

Let’s take a look at each of the fields one by one:

  • General

    • Theme Slug: Enter your theme’s slug into this field in all lowercase letters, numbers and/or underscores. This value will be used to prefix functions and variables that would otherwise be incompatible with the WordPress core or any installed themes or plugins. Typically, this is the same string as the text domain, with all hyphens replaced by underscores.
    • Unique Query Variable Identifier: Enter a descriptive name for your query variable. It will be used to identify the query variable. The query variable identifier should be a unique name in all lowercase letters, numbers and/or underscores.
    • Include the Loop: Check this box if you’d like to have the code in the loop be processed on each post. By default, this value is set to false.
  • Post/Page

    • Post ID: Enter the post ID in this field. It should be an integer value.
    • Post Slug: Enter the post slug here. It should be in all lowercase letters, numbers, and/or underscores.
    • Post Title: Enter the post title value in this text field. It is available with WordPress 4.4 and above.
    • Post Parent: The page ID entered in this field will return only the child pages. To return only the top-level entries and excluding all child pages, set this value to 0. This field accepts an int. value.
    • Page ID: Enter the page ID in this field. This field accepts an int. value.
    • Page Name: Enter the page slug here. It should be in all lowercase letters, numbers, and/or underscores.
    • Post Type: Enter a string/array to retrieve posts by post types. The default value is post.
    • Post Status: Enter a string/array to retrieve posts by post status. The default value is publish.
  • Pagination

    • No Paging: Determines whether to show all posts or use pagination. By default, it’s set to false, i.e. use pagination.
    • Post to Show Per Page: Enter the number of posts you’d like to have displayed on each page. The value -1 shows all posts.
    • Post to Show Per Archive Page: Enter the number of posts to show per page on archive pages only. It overrides posts_per_page and showposts on pages where the functions is_archive() or is_search() return true.
    • Offset: Set the number of posts to displace or pass over. It will ignore the paged parameter and break pagination. Note: this parameter is ignored when posts_per_page is set to -1.
    • Paged: Enter the number of page. It will show the posts that would normally show up on a given page when the Older Entries link is used.
    • Page: Defines the number of page for a static front page. It will show the posts that would normally show up on a given page of a static front page.
    • Ignore Post Stickiness: This boolean value determines whether or not to ignore post stickiness. By default, the value is set to false, which means that the sticky posts will be moved to the start of the set. If you set it to true, the sticky posts will not be moved to the start of the set.
  • Author

    • Author ID: Enter the author ID here. You can exclude authors by ID using the minus (-) sign e.g. -1.
    • Author Name: Enter a string value here using “user_nicename”; not name.
  • Password

    • Has Password: This boolean value determines whether or not to show posts with passwords. Setting it to true means it will show posts with passwords. And setting it to false means it will show posts without passwords. Leaving it null will display all posts with and without passwords for WordPress 3.9 and above.
    • Post Password: Setting this argument displays posts with the specified password. This functionality is compatible with WordPress 3.9 and above.
  • Misc.

    • Search String: Display posts based on a searched keyword. This text field accepts a string value.
    • Display Order: Select the display order for the orderby parameter. Defaults to Descending.
    • Order By: Orders the received posts by the specified argument. The possible values are listed above. The value defaults to date.
    • Custom Field Key: Enter a unique key identifier. This field accepts a string value.
    • Custom Field Value (string): Enter a custom field value to show all posts associated with the specified custom field. This field accepts a string value.
    • Custom Field Value (num): Enter a custom field value to show all posts associated with the specified custom field. This field accepts a number value.
    • Meta Compare: Enter an operator to test the meta_value. You can select one value from the given options.
    • User Permission: Display published or private posts is the user has appropriate permissions. Enter user permission such as readable or editable.
  • Cache

    • Post Information Cache: Displays posts with (true) or without (false) caching post information. Default value is set to true.
    • Post Meta Information Cache: Display posts with (true) or without (false) caching post meta information. Default value is set to true.
    • Post Term Information Cache: Display posts with (true) or without (false) caching post term information. Default value is set to true.

How Do I Add the Code Snippets to My Website?

Download the theme’s functions.php file to your desktop via an FTP client. Next, open it up in a text editor and paste the code at the bottom of the file. Hit the Save button and re-upload it to your theme’s directory. Once you’re done, the newly added custom post status will be available for use.

Alternatively, you can add the generated code snippets to your site is by pasting them directly into your current theme’s functions.php file. You can edit it directly by heading over to Appearance > Editor from the admin panel.

Conclusion

WP_Query class is an incredibly powerful tool that allows developers to create custom queries to make their WordPress theme’s behave exactly as they want them to. Hopefully, this code generator tool will save you some time from having to write custom code from scratch.

Do you have any questions about the WP_Query code snippet generator tool? Let us know in the comments section below!


Written exclusively for

Nimbus Themes Publishing Logo

About the Author

Evan Scoboria is the co-founder, and developer at Shea Media LLC, the team behind Nimbus Themes, this magazine, and a bunch of very happy clients. He co-founded Shea Media with his wife Kendall in 2009. Evan enjoys hunting, fishing, code, cycling, and most of all WordPress!

Read all posts

Leave a Reply

Your email address will not be published. Required fields are marked *