WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying Posts Using a Custom Select Query (3 posts)

  1. arien101
    Member
    Posted 5 years ago #

    Hi,

    I have a page that is supposed to display its subpages in a list format in the main content area. All the subpages have custom fields, so that I can select which pages I consider subpages. The custom fields are "page_type" and "region_name".

    What I want to do is to select all "page_type = tauchbasis", but only if they are in "region_name = hurghada".

    I have created a template for this page type and added the code I found here: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

    Here is the code:

    <?php
     $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'page_type'
        AND wpostmeta.meta_value = 'tauchbasis'
        AND wposts.post_status = 'publish'
        AND wposts.post_type = 'page'
        ORDER BY wposts.post_date DESC
     ";
    
     $pageposts = $wpdb->get_results($querystr, OBJECT);
     ?>
    
     <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
        <div class="tauchliste" id="post-<?php the_ID(); ?>">
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanenter Link zu <?php the_title(); ?>">
          <?php the_title(); ?></a></p>
        </div>
      <?php endforeach; ?>
    
      <?php else : ?>
        <br />
        <h2 class="center">Nicht gefunden</h2>
        <p class="center">Sorry, aber es wurden keine Eintr&auml;ge gefunden.</p>
        <?php include (TEMPLATEPATH . "/searchform.php"); ?>
     <?php endif; ?>

    The result is nearly what I want. It lists all the pages with "page_type = tauchbasis". What I now tried to do is this:

    <?php
     $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'page_type'
        AND wpostmeta.meta_value = 'tauchbasis'
        AND wpostmeta.meta_key = 'region_name'
        AND wpostmeta.meta_value = 'hurghada'
        AND wposts.post_status = 'publish'
        AND wposts.post_type = 'page'
        ORDER BY wposts.post_date DESC
     ";
    
     $pageposts = $wpdb->get_results($querystr, OBJECT);
     ?>

    Unfortunately this does not produce any results.

    Any pointers on how I can select only the posts with "region_name = hurghada"?

    Thanks!

  2. arien101
    Member
    Posted 5 years ago #

    Ok, so I realized that using custom fields for this is not the best way to do it. Instead I am now using 2 separate Loops and pulling the pages based on their tags. Like this:

    <?php query_posts('tag=tauchbasis+Hurghada') ?>
    
    <?php rewind_posts(); ?>
    <?php $my_query = new WP_Query('tag=tauchbasis+Hurghada&orderby=title&order=ASC'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <div><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanenter Link zu'); ?><?php the_title(); ?>"><?php the_title(); ?>
    </a></div>		
    
    <?php endwhile; ?>

    At this time however, the tags by which the posts are filtered are still hardcoded, e.g. "tag=tauchbasis+Hurghada".

    What I want to do instead is pass the value of $query_tags as tags. $query_tags would then contain "tauchbasis+Hurghada". Here's what I tried but didn't work:

    <?php query_posts('tag=&query_tags') ?>
    
    <?php rewind_posts(); ?>
    <?php $my_query = new WP_Query('tag=&query_tags&orderby=title&order=ASC'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

    Could someone point out what I did wrong?

  3. arien101
    Member
    Posted 5 years ago #

    Should you have a similar problem: You could either try the above...

    ...or you could just save yourself a lot of trouble and instead use this:

    <?php
      $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
      if ($children) { ?>
      <ul>
      <?php echo $children; ?>
      </ul>
      <?php } ?>

    I realized I could just show the subpages of the page I was on instead of using the hard way with tags...

    Maybe this'll help someone someday.

Topic Closed

This topic has been closed to new replies.

About this Topic