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 11 replies - 16 through 26 (of 26 total)

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/page/2/#post-2102608)
 * I’m sorry but you’ve lost me now…
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/page/2/#post-2102609)
 * OK … here is the Scenario …
 * I have a published ‘About Us’ page. It’s post_status is ‘publish’. Its currently
   on revision 9.
 * This page is Live …
 * I then make an edit to the ‘About Us’ page, I change the post status to ‘draft’.
   It is now on revision 10.
 * When my users visit the site, currently the way WordPress works is my ‘About 
   Us’ page would not be visible becuase it’s status is ‘draft’. I want to customize
   the loop to show that last page that had a post_status of ‘publish’.
 * So that way my users see version 9 of ‘About Us’ while I work on version 10 in
   private.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/page/2/#post-2102610)
 * Ah – I see. I don’t think you can do this. When you set the post_status to draft,
   you over-write the ‘publish’ post status.
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/page/2/#post-2102611)
 * Ok …
 * So then to get on to that stream, where is the post_status set?
 * I am guessing that it happens any time a post/page/custom type is created or 
   updated.
 * Now the question is … in what file does that happen?
 * Because if I can find that, I can modify it to not update the post_type and then
   it can be searched using our queries above.
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/page/2/#post-2102612)
 * Seems a lot of work, instead of keeping loads of revisions why not just keep 
   the same page twice.
 * One published one as a permanent draft, when happy with the draft revision preview,
   just copy and paste the html to overwrite the published page and press update.
 * David
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/page/2/#post-2102613)
 * This is for a corporate implementation … and people want to see what a page is
   going to look like in context of the rest of the website.
 * I know this may sound strange but its a big requirement when using this as a 
   CMS … at least for our company.
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/page/2/#post-2102614)
 * Ok then keep the page as published in page.php call a function to return the 
   last post revision with the post parent with the post->id.
 * This is just mock code for structure, you want visitors to see the latest revision,
   logged in users or admin to see the published post.
 * The page will not appear in the menu until it is published, so switch to the 
   last revision in page.php
 * **UNTESTED:**
 *     ```
       <?php
       if( !is_user_logged_in() ) {
       	$args = array(
       		'post_parent' => $post->ID,
                       'post_type' => 'revision',
       		'post_status' => 'inherit',
       		'numberposts' => 1
       	);
       	$revision = get_posts( $args );
       	if ($revision) {
       		$post = $revision;
       	}else{
       		rewind_posts();
       	}
       }
       ?>
       ```
   
 * Not sure about this `$post = $revision;` or `$posts = $revision;` or `$post[0]
   = $revision[0]` as an array is returned.
 * Might not need the rewind when using `get_posts()`?
 * HTH
 * David
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/page/2/#post-2102615)
 * Wow … Let me take some time to soak this in … but this looks fantastic!
 * Thanks so much …
 * Mike
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/page/2/#post-2102616)
 * Ok … I am not sure I understand.
 * How would I set this up with the actual page … If ‘About Us’ has to be edited
   but not published … how would the previous revision be viewed?
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/page/2/#post-2102617)
 * Ah ok,
    I read again the problem with using the post meta as a flag is that you
   would have to remember which revision you have it set on, or just find the first.
 * Lets say you had a custom field ‘published’, if this is set then that revision
   is ‘good to go’, again this is mock code and might have bugs!
 * Loop to find the first (latest) revision with a custom field of published with
   a value.
 *     ```
       <?php
       if( !is_user_logged_in() ) {
       	$args = array(
       		'post_parent' => $post->ID,
       		'post_type' => 'revision',
       		'post_status' => 'inherit'
       	);
       	$revisions = get_posts( $args );
       	if($revisions) {
       		foreach $revision as $revision {
       			if ( get_post_meta($revision->ID,'published',true) ) {
       				$my_post = $revision;
       				break;
       			}
       		}
       	}
       }
       ?>
       ```
   
 * Then you would have to code to set the $post to my_post;
 * You could also code inside the loop to test the page meta, output the post and
   return!
 * Just bouncing ideas!
 * David
 *  Thread Starter [mdumka](https://wordpress.org/support/users/mdumka/)
 * (@mdumka)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/wordpress-loop-question/page/2/#post-2102619)
 * Ok … Mind has been swirling a bit and I think I have something but first … how
   is a page called?
 * So you have this page … [http://example.com/?p=10](http://example.com/?p=10) 
   where the page/post ID is 10.
 * How does that get dumped into the loop?

Viewing 11 replies - 16 through 26 (of 26 total)

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

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
