Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End MySQL Query usage within WP Page Template

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #32721
    Eamonn
    Member

    Hi Guys.

    I’m trying to generate a list of all pages on my site where a custom field value is equal to ‘X’ (or whatever, it’ll be a variable). To achieve this, I’m trying to use a MySql query within the page template. I have looked their usage up in the WP Codex, but frankly don’t fully understand the over-involved explainations given.

    Dropping the $wpdb class and running a basic query works fine:

    
    $results = mysql_query("
    SELECT post_title
    FROM wp_posts");

    while($row = mysql_fetch_array( $results )) {
    echo $row . '
    ';
    }
    ?>

    but when I try to develop that query, I either get a complete list of posts (without filtering) or nothing at all:

    
    $results = mysql_query("
    SELECT wp_posts.post_title
    FROM wp_postmeta,wp_posts
    WHERE wp_postmeta.meta_value = 'X'");

    while($row = mysql_fetch_array( $results )) {
    echo $row . '
    ';
    }
    ?>

    It could very well be that I’m just crap at MySQL ;)

    If anyone has any ideas – or can actually explain the whole $wpdb thing properly – I’d be very grateful.

Viewing 1 post (of 1 total)
  • The forum ‘Back End’ is closed to new topics and replies.