rtCamp notes, day 17 of undefined

What is WP QUERY?

WP Query is a class inbuilt in WordPress which will help us,

know the current query a page is running or initiate a new query to get data from database.

How to use WP QUERY?

$query = new WP_QUERY( $arguments );

Here we have to provide arguments to WP_QUERY

some of the arguments are listed below

  • Author parameters – To filter on the basis of authors
author - add author ids (string -> seprated with comma inside string)
author_name - user nick name
author__in - (array) author id
author__not_in - (array) author id
  • Category parameters – To filter on the basis of categories
cat - (int) category id [will display children category also]{use minus (-) in front of id to exclude category}

category_name - category slug (comma seprated string)

category_name - (cat_1 + cat_1) -> which have all the category listed

category__and - (array) posts from listed categories

category__in - (array) 

category__not_in - (array) posts which dont have these categories
  • Tag parameters – To filter on the bassi of tags
tag - (string) tag slug

tag_id - (int)

tag__and - (array) tag ids

tag__in - (array) tag_ids

tag__not_in - array -> tag ids

tag_slug__and - array() -> tag slugs

tag_slug__in - array() -> tag slugs
  • Taxonomy – To filter on the basis of taxonomies
tax_query - array() -> use the below parameters in this

-- need more understanding and implementing -- 
  • Post & Page -> filter on the basis of page and psot
p -> int() post id

name - string -> post slug

title - (string) -> post title

page_id -> (int) -> page_id

pagename -> (string) page-slug

post_name__in -> (array) -> post slug

post_parent -> (int) -> returns child pages

post_parent__in -> array() -> returns childs page

post_parent__not_in -> array() -> returns child pages

post__in -> array() -> returns posts

post__not_in -> (array) -> return posts



post_type -> (array | string) -> display CPT -> default : post



post_status -> (array | string) -> display posts according to status - publish, pending, trash etc

We can access the current query variable with the global variable global $wp_query

Methods in WP_QUERY (cheatsheet)

get_posts — Retrieves an array of posts based on query variables.
get_queried_object — Retrieves the currently queried object.
get_queried_object_id — Retrieves the ID of the currently queried object.
get_search_stopwords — Retrieve stopwords used when parsing search terms.
have_comments — Whether there are more comments available.
have_posts — Determines whether there are more posts available in the loop.
init — Initiates object properties and sets default values.
init_query_flags — Resets query flags to false.
is_404 — Is the query a 404 (returns no results)?
is_archive — Is the query for an existing archive page?
is_attachment — Is the query for an existing attachment page?
is_author — Is the query for an existing author archive page?
is_category — Is the query for an existing category archive page?
is_comment_feed — Is the query for a comments feed?
is_comments_popup — Whether the current URL is within the comments popup window. — deprecated
is_date — Is the query for an existing date archive?
is_day — Is the query for an existing day archive?
is_embed — Is the query for an embedded post?
is_favicon — Is the query for the favicon.ico file?
is_feed — Is the query for a feed?
is_front_page — Is the query for the front page of the site?
is_home — Is the query for the blog homepage?
is_main_query — Is the query the main query?
is_month — Is the query for an existing month archive?
is_page — Is the query for an existing single page?
is_paged — Is the query for a paged result and not for the first page?
is_post_type_archive — Is the query for an existing post type archive page?
is_preview — Is the query for a post or page preview?
is_privacy_policy — Is the query for the Privacy Policy page?
is_robots — Is the query for the robots.txt file?
is_search — Is the query for a search?
is_single — Is the query for an existing single post?
is_singular — Is the query for an existing single post of any post type (post, attachment, page, custom post types)?
is_tag — Is the query for an existing tag archive page?
is_tax — Is the query for an existing custom taxonomy archive page?
is_time — Is the query for a specific time?
is_trackback — Is the query for a trackback endpoint call?
is_year — Is the query for an existing year archive?
lazyload_comment_meta — Lazyload comment meta for comments in the loop. — deprecated
lazyload_term_meta — Lazyload term meta for posts in the loop. — deprecated
next_comment — Iterate current comment index and return WP_Comment object.
next_post — Set up the next post and iterate current post index.
parse_order — Parse an ‘order’ query variable and cast it to ASC or DESC as necessary.
parse_orderby — Converts the given orderby alias (if allowed) to a properly-prefixed value.
parse_query — Parse a query string and set query type booleans.
parse_query_vars — Reparse the query vars.
parse_search — Generates SQL for the WHERE clause based on passed search terms.
parse_search_order — Generates SQL for the ORDER BY condition based on passed search terms.
parse_search_terms — Check if the terms are suitable for searching.
parse_tax_query — Parses various taxonomy related query vars.
query — Sets up the WordPress query by parsing query string.
reset_postdata — After looping through a nested query, this function restores the $post global to the current post in this query.
rewind_comments — Rewind the comments, resets the comment index and comment to first.
rewind_posts — Rewind the posts and reset post index.
set — Sets the value of a query variable.
set_404 — Sets the 404 property and saves whether query is feed.
set_found_posts — Set up the amount of found posts and the number of pages (if limit clause was used) for the current query.
setup_postdata — Set up global post data.
the_comment — Sets up the current comment.
the_post — Sets up the current post.

Meta boxes

Meta boxes are the boxes which are shown in the right side of the editor, for e.g. Categories, Tags, Featured Image, etc.

META -> data for data, they help us store some additional data for our posts

How to add a meta box?

add_meta_box('meta_box_id', 'Meta_box_title', 'callback_function_for_html', 'screen');

Leave a Reply

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