Support » Fixing WordPress » sort by category and custom field

  • eatrains


    I’m trying to make a loop that will display posts that belong to a certain category and have a certain custom field value. I’ve been able to get the custom field part working using However, I don’t know how to get the category part working. I’ve tried a few of things, but either it gives me an error or displays 20 copies of the same post. Can someone please help me?

Viewing 4 replies - 1 through 4 (of 4 total)
  • 3stripe


    What’s the code you are using then?



    I am using the code from that Codex page:

    $pageposts = $wpdb->get_results("SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'tag' AND wpostmeta.meta_value = 'email' AND
    wposts.post_status = 'publish' AND wposts.post_date < NOW() ORDER BY wposts.post_date DESC", OBJECT);
    <?php if ($pageposts): ?>
    <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>

    <div class="post" id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a></h2>
    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    <div class="entry">
    <?php the_content('Read the rest of this entry »'); ?>

    <p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>
    <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    <?php endforeach; ?>

    <?php else : ?>
    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>
    <?php include (TEMPLATEPATH . "/searchform.php"); ?>
    <?php endif; ?>

    Is there a way to add to this so that it also shows posts from a certain category in addition to the postmeta restriction?





    Kaf to the rescue again!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘sort by category and custom field’ is closed to new replies.