WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display Posts filtered by Custom Fields (2 posts)

  1. Paracetamol
    Member
    Posted 7 years ago #

    Hello!
    I'm using custom field meta (boolean) to tag posts for display on the front page. (with the help of Custom Field GUI)

    For doing so, I copied the code for an alternative loop as described in this post. This worked out better than other solutions I've read of, I can even continue using template tags!

    Sadly, the single template tag not being display is the_content();! I suppose this has something to do with the database query, but that's just a guess. Is there any way to make this display the_content();?

    <?php
    $customkey = 'Main'; // set to your custom key
    global $wpdb;
    $my_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts, $wpdb->postmeta WHERE ID = post_id AND meta_key = '$customkey' ORDER BY post_date DESC LIMIT 1");
    foreach($my_posts as $post) :
    ?>
    <!-- my template here -->
    <?php endforeach; ?>

    Edit: Sorry, I just realized, the original post is only a week old. You can probably still post there.

  2. Paracetamol
    Member
    Posted 7 years ago #

    I made it! Don't know why, but this one's working perfectly:


    <?php $pageposts = $wpdb->get_results("SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'Main' ORDER BY post_date DESC LIMIT 1", OBJECT); ?>
    <?php if ($pageposts) : foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>
    //Content of the Loop//
    <?php endforeach;?>
    <?php endif; ?>

    The code derives from this post with alternative ORDER parameter.

    It displays the single latest post with the custom field "Main" set TRUE on my home.php template.

Topic Closed

This topic has been closed to new replies.

About this Topic