Title: WordPress Loop Question
Last modified: August 20, 2016

---

# WordPress Loop Question

 *  [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/)
 * Hello,
 * I was wodering if someone could help me with a loop question …
 * Now I know that the loop gets all of the latest versions of pages/posts.
 * But what If I wanted the loop to get the latest versions based on a custom field
   value?
 * So for example …
 * 1) Page = ‘About Us’ — Page revision 17 — Custom Field Value = ‘True’
 * 1) Page = ‘About Us’ — Page revision 18 — Custom Field Value = ‘False’
 * I would like the loop to return latest revisons on all pages/posts with a Custom
   Field Value of ‘True’ meaning page revision 17 would be the page shown, not revision
   18.
 * Thanks for all of the help …
 * Mike

Viewing 15 replies - 1 through 15 (of 26 total)

1 [2](https://wordpress.org/support/topic/wordpress-loop-question/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/wordpress-loop-question/page/2/?output_format=md)

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102170)
 * [http://codex.wordpress.org/Function_Reference/query_posts](http://codex.wordpress.org/Function_Reference/query_posts)
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102246)
 * Thanks Esmi …
 * But in looking at the database, say I have a page that has 10 revisions. Only
   one has a post type of ‘page’ and the revisions have a post type of ‘revision’.
 * How would the query posts function help solve my problem?
 * Thanks
 * Mike
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102287)
 * If you want to display post revisions, yes.
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102389)
 * Ok … so you have to bear with me as all this information is new.
 * First I have to place the following before ‘The Loop’
 * // The Query
    query_posts( $args );
 * Next … I have to supply the arguments or parameters to query_posts(). So based
   on the fact I want a page or any of its revisions I would use this …
 * ‘post_type=any’
 * I will wait to muck it up any more until your reply …
 * Thanks
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102410)
 * No – `post_type=any` won’t work. Have a look at [this Codex page](http://codex.wordpress.org/Function_Reference/WP_Query#Type_.26_Status_Parameters)
   for examples of the parameters you can use in query_posts.
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102470)
 * Ok so ‘any’ does not include revisions so do i search for post_type that includes
   page and revision to make sure i am looking for everything?
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102598)
 * Hi Esmi …
 * I have been doing some more research, so I think this is what I need to find 
   all revisions of a page ..
 * `$query = new WP_Query( array( 'post_type' => array( 'page', 'revision' ) ) );`
 * Does that look right?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102599)
 * That will give you all published Pages **and** all revisions (posts & pages).
   Is that what you want?
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102600)
 * Nope …
 * Say I have a page ‘About Us’ … I want the page and all of its revisions.
 * That’s the 1st part of my Query …
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102601)
 * I think you’ll need to do that in 2 parts. The first query/loop would be a standard
   one that pulls in the published page. The second query/loop would be needed for
   the revisions. Try:
 *     ```
       $args = array (
       	'pagename' => 'About Us',
       	'post_status' => 'inherit'
       );
       $rev_query = new WP_Query($args );
       ```
   
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102602)
 * Well now is where is gets tricky …
 * The whole reason for this custom loop is so I can work on published pages and
   posts with out having those changes become instantly live.
 * So for example … I would like to limit the query to return 1 row/record or the
   last page/post with a post_status of ‘published’.
 *     ```
       $args = array (
       	'pagename' => 'About Us',
       	'post_status' => 'Published',
       	'posts_per_page' = 1
       );
       $rev_query = new WP_Query($args );
       ```
   
 * So that query should return the latest published revision of ‘About Us’ … and
   it should not matter if its a page/post or revision … correct?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102604)
 * Try:
 *     ```
       $args = array (
       	'post_type' => array('any', 'revision');
       	'orderby' => 'date'
       	'posts_per_page' => 1
       );
       $query = new WP_Query($args );
       ```
   
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102605)
 * Ok … but what if I have version 9 as published, but version 10 as draft?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102606)
 * Drafts are covered by ‘any’. Only revisions are excluded from the ‘any’ parameter.
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/#post-2102607)
 * Ok … Now I completely understand the last query.
 * But now in looking at my database … every revision of any page/post other then
   the published one has a post status of ‘inherit’ if it is not ‘publish’.
 * I need to keep a record of the statuses … how do I do that so I can query against
   it?

Viewing 15 replies - 1 through 15 (of 26 total)

1 [2](https://wordpress.org/support/topic/wordpress-loop-question/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/wordpress-loop-question/page/2/?output_format=md)

The topic ‘WordPress Loop Question’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 26 replies
 * 3 participants
 * Last reply from: [mdumka](https://wordpress.org/support/users/mdumka/)
 * Last activity: [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/page/2/#post-2102619)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
