WordPress.org

Ready to get started?Download WordPress

Forums

Querystring for pages (not posts) (5 posts)

  1. Rics1983
    Member
    Posted 3 years ago #

    Hi to all, i hope that someone can help me to understand better the querystring for pages.

    I've succesfully used the querystring code showed here in the codex:

    $querystr = "
    	SELECT * FROM $wpdb->posts
    	LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    	LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    	LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    	LEFT JOIN $wpdb->terms ON($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id)
    	WHERE $wpdb->terms.name = 'slides'
    	AND $wpdb->term_taxonomy.taxonomy = 'category'
    	AND $wpdb->posts.post_status = 'publish'
    	AND $wpdb->posts.post_type = 'post'
    	AND $wpdb->postmeta.meta_key = 'order'
    	ORDER BY $wpdb->postmeta.meta_value ASC
    ";

    Ok, that's for query the posts, but what i would be glad to know is how to obtain the result with a similar code not for posts but for pages.
    The following example, got always from the codex page, is working, but not extensively as the previous:

    $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'custom-key'
        AND wposts.post_type = 'page'
        ORDER BY wpostmeta.meta_value DESC
        ";

    Can someone help me?
    Thank you!

  2. esmi
    Forum Moderator
    Posted 3 years ago #

  3. Rics1983
    Member
    Posted 3 years ago #

    Thank you for the reply esmi!
    I've already checked that codex page, but i haven't success trying with the "query_posts" function, probably because i'm using meta keys and values to retrieve pages and to order them via different methods.

    I think that's better if i write the code i'm using, the first to list a series of posts title&thumbnail on a page, the second one to list a series of pages title&thumbnails on another page.
    The last one obviously haven't the sort order working correctly as the first code example.

    Code for displaying a series of posts title&thumbnail in a page with two links that produce a different sorting (full working):

    [Code moderated as per the Forum Rules. Please use the pastebin]

    (Sorry for the longer code and post)

  4. Rics1983
    Member
    Posted 3 years ago #

    Sorry, for the previous post, here are the pastebins.

    The first code, to show posts list with title and thumbnails in a page, with two succesfully working links for sorting the list is the following:
    Code for posts (on pastebin)

    The second code, to show pages list with title and thumbnails in a page, with the two not working links for sorting the list is the following:
    Code for pages (on pastebin)

    By the way today i have learned a bit more about SQL via the w3schools website, so i've tried to access to the wpdb with my way for the pages query:

    $querystr =
            "SELECT wposts.*
    	FROM
            $wpdb->posts wposts,
            $wpdb->postmeta wpostmeta,
            $wpdb->terms wterms
            WHERE wpostmeta.meta_value = 'Gaming'
    	AND wposts.post_type = 'page'
    	AND wterms.name = 'Gaming Design'
    	ORDER BY wpostmeta.meta_value ASC";
    	$pageposts = $wpdb->get_results($querystr, OBJECT);

    If i don't get wrong, this will get the pages from the wpdb where the meta_value is equal to Gaming and where the category is Gaming Design.

    But what i've on the output are all the pages, including the pages that haven't meta_value equal to Gaming and also the pages that have category not equal to Gaming Design.

    Probably i wrote something wrong, but what?

    Thank you for the help!

  5. Rics1983
    Member
    Posted 3 years ago #

    Today i've tried other ways but got no solutions so far.
    I'm stuck at the previous post.

Topic Closed

This topic has been closed to new replies.

About this Topic