Query posts in WordPress

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/

Advertisements

About admin

Just another php developer trying to give something back to the community.
This entry was posted in Wordpress and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s