WordPress.org

Ready to get started?Download WordPress

Forums

sort by category and custom field (5 posts)

  1. eatrains
    Member
    Posted 8 years ago #

    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 http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query. 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?

  2. 3stripe
    Member
    Posted 8 years ago #

    What's the code you are using then?

  3. eatrains
    Member
    Posted 8 years ago #

    I am using the code from that Codex page:

    <?php
    $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 »'); ?>
    </div>

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

  4. Kafkaesqui

    Posted 8 years ago #

  5. 3stripe
    Member
    Posted 8 years ago #

    Kaf to the rescue again!

Topic Closed

This topic has been closed to new replies.

About this Topic