Computing Magazine

Query Posts in WordPress

Posted on the 29 April 2014 by Akahgy

Description:

Querying through the posts can be useful for creating special container pages. Returning the posts you require is only a matter of using the right attributes.

Solution:

A query in WordPress looks like a series of GET variables:

query_posts(‘posts_per_page=-1&cat=’.7.’&orderby=title&order=ASC’)

This will return all posts that are in category 7, and will order them alphabetically.

WordPress offers also other functions that help query for the right posts:

have_posts(); – boolean, determines if the query was successful

the_post(); – focuses on one post from the iteration, allows different operations

get_the_category(); – returns the category of the post

get_the_title(); – returns the title of the focused post

More example under: https://codex.wordpress.org/Function_Reference/


Back to Featured Articles on Logo Paperblog

Magazine