John Davidson

php - A WP Query for Posts and Pages but only returns 1 posts/pages

0 comments
Message:


I'm trying to create a wp query that goes through posts and pages and only returns one (the most recent, being a post or a page). So far, I have this:


$conteudo_complexo = new WP_Query(array(
'post_type' => array('post', 'page'),
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC',
'meta_key' => 'tipo_de_destaque_homepage',
'meta_value' => 'topoeditorial'
));

This is returning multiple pages...


What I'm looking for is something like: go through posts and pages and return only one post or page with that meta_value, whichever is the most recent one.


Note: The meta_key and meta_value is for custom fields.



My Answer:
> 'any',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
));

This query will return the most recent post or page, regardless of whether it is a post or a page. The 'post_type' parameter set to 'any' allows the query to search through both posts and pages. The 'posts_per_page' parameter is set to 1 to only return one result. The 'orderby' parameter is set to 'date' to order the results by date, and the 'order' parameter is set to 'DESC' to return the most recent result.

Rate this post

5 of 5 based on 5062 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us