Contents
Displaying Featured Results with WP_Query
WordPress’ WP_Query class is the cornerstone of custom post retrieval. When your theme or plugin requires a custom selection of posts—such as “Featured” items—you’ll often turn to WP_Query. This article explores in depth how to display featured results, covering query parameters, template integration, performance considerations, and best practices.
1. Understanding “Featured” in WordPress
“Featured” is a context-dependent term. You might define featured posts by:
- A custom meta field (e.g.
_is_featured
set to true) - A dedicated category or tag (taxonomy-based)
- Post format, taxonomy, or custom post type
Choosing the right storage mechanism impacts your query logic:
Method | Pros | Cons |
---|---|---|
Custom Meta Field | Flexible, easily toggled in UI | Requires meta_query, can impact performance at scale |
Category/Tag | Built-in UI, no overhead of meta query | Potentially cluttered taxonomy list |
Custom Post Type | Clear separation, easy templating | Requires registering a new CPT, admin menu adjustments |
2. Fundamentals of WP_Query
WP_Query accepts an array of query parameters. Some key ones are:
post_type
– restrict to post, page, or custom typemeta_key
andmeta_value
– filter by a single meta fieldmeta_query
– for complex meta-based filteringtax_query
– for taxonomy-based selectionposts_per_page
– number of items to returnorderby
andorder
– sorting rulespaged
– for pagination
Full reference: WP_Query Class
3. Querying Featured Posts via Meta Field
Suppose you’ve added a checkbox in the post editor that updates the _is_featured
meta key to 1
when checked. Here’s how to query and display those posts:
ltphp
featured_args = array(
post_type =gt post,
posts_per_page =gt 5,
meta_query =gt array(
array(
key =gt _is_featured,
value =gt 1,
compare =gt =
)
),
orderby =gt date,
order =gt DESC,
)
featured_query = new WP_Query( featured_args )
gt
Loop through results:
ltphp if ( featured_query-gthave_posts() ): >
ltul class=featured-list style=list-style:none padding:0gt
ltphp while ( featured_query-gthave_posts() ): featured_query-gtthe_post() >
ltli style=margin-bottom:15pxgt
lth4 style=margin:0 0 5pxgtlta href=ltphp the_permalink() gt style=text-decoration:none color:#0073aagtltphp the_title() gtlt/agtlt/h4gt
ltp style=margin:0 color:#666gtltphp the_excerpt() gtlt/pgt
lt/ligt
ltphp endwhile wp_reset_postdata() gt
lt/ulgt
ltphp else: >
ltpgtNo featured posts found.lt/pgt
ltphp endif gt
4. Taxonomy-Based Featured Results
If you use a “Featured” category, your query is simpler and more efficient:
tax_args = array(
post_type =gt post,
posts_per_page =gt 5,
tax_query =gt array(
array(
taxonomy =gt category,
field =gt slug,
terms =gt featured
)
)
)
featured_tax_query = new WP_Query( tax_args )
Tax queries can also be AND or OR combined under more complex logic see Taxonomy Parameters.
5. Pagination and Offset
When displaying featured results with pagination:
paged = get_query_var( paged ) get_query_var( paged ) : 1
args = array(
post_type =gt post,
meta_key =gt _is_featured,
meta_value =gt 1,
posts_per_page =gt 10,
paged =gt paged,
)
query = new WP_Query( args )
Then use previous_posts_link()
and next_posts_link()
for navigation:
ltdiv class=pagination style=margin-top:20pxgt
ltdiv style=float:leftgtltphp previous_posts_link( amplaquo Newer Featured ) gtlt/divgt
ltdiv style=float:rightgtltphp next_posts_link( Older Featured ampraquo, query-gtmax_num_pages ) gtlt/divgt
ltdiv style=clear:bothgtlt/divgt
lt/divgt
6. Ordering and Prioritizing Featured Items
To allow custom ordering of featured posts, you can add a numeric meta field _featured_order
. Then:
order_args = array(
post_type =gt post,
posts_per_page =gt -1,
meta_query =gt array(
array(
key =gt _is_featured,
value =gt 1
)
),
orderby =gt meta_value_num,
meta_key =gt _featured_order,
order =gt ASC
)
ordered_query = new WP_Query( order_args )
7. Performance Considerations
- Meta queries can be slower on large datasets. Consider Query Monitor to benchmark.
- Use
WP_Query
caching or object caching with Transient API when featured results change infrequently. - For high-traffic sites, precompute featured lists via a scheduled task and store in transient to avoid repeated queries.
8. Template Integration
front-page.php
or a custom template part (template-parts/featured.php
) to maintain separate concerns.
9. Best Practices
- Always call
wp_reset_postdata()
after a custom loop. - Use non-conflicting meta keys (prefix with underscore, e.g.
_mytheme_featured
). - Avoid
query_posts()
instantiatenew WP_Query()
instead. - Document your query logic so designers and editors understand “featured” definitions.
10. Further Resources
By mastering WP_Query for featured results, you’ll equip your themes and plugins with powerful, flexible content displays—enhancing both performance and editorial control.
|
Acepto donaciones de BAT's mediante el navegador Brave 🙂 |