Support » Fixing WordPress » 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 …


Viewing 15 replies - 1 through 15 (of 26 total)
  • 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?



    If you want to display post revisions, yes.

    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 …


    I will wait to muck it up any more until your reply …


    No – post_type=any won’t work. Have a look at this Codex page for examples of the parameters you can use in query_posts.

    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?

    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?

    That will give you all published Pages and all revisions (posts & pages). Is that what you want?

    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 …

    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 );

    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?


    $args = array (
    	'post_type' => array('any', 'revision');
    	'orderby' => 'date'
    	'posts_per_page' => 1
    $query = new WP_Query($args );

    Ok … but what if I have version 9 as published, but version 10 as draft?

    Drafts are covered by ‘any’. Only revisions are excluded from the ‘any’ parameter.

    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)
  • The topic ‘WordPress Loop Question’ is closed to new replies.