WordPress.org

Ready to get started?Download WordPress

Forums

Sidebar post filter (3 posts)

  1. eian00
    Member
    Posted 2 years ago #

    Hello,

    I have some problems making a post filter in my sidebar..
    I would need to display all the recent posts that are from the same category as the current post, and must have the same value from a custom field as the current displayed post.

    I have tried something like that, but thn there is the category problem

    <?php $cur_post_value = get_post_meta($post->ID, 'town', true);
    
    $other_posts_with_value = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'town'
        AND wpostmeta.meta_value = '".$cur_post_value."'
        AND wposts.post_status = 'publish'
        AND wposts.post_type = 'post'
        ORDER BY wposts.post_date DESC
        LIMIT 4
     ";
    $other_posts_array = $wpdb->get_results($other_posts_with_value,
     OBJECT); ?>
    
    <?php if ($other_posts_array): ?>

    Thank you

  2. hotplugin
    Member
    Posted 2 years ago #

    can this help?

    $category = get_the_category();
    if (!empty($category))
    $cat_tree = get_category_parents($category[0]->term_id, FALSE, ':', TRUE);
    $top_cat = split(':',$cat_tree);
    $cat_slug = $top_cat[0];

    query_posts('posts_per_page=15&category_name='.$cat_slug.'&orderby=rand');

  3. eian00
    Member
    Posted 2 years ago #

    and how would it show the posts with that category and with the same value from a custom field?

    now there should be a way to join mine and your code, but how?

Topic Closed

This topic has been closed to new replies.

About this Topic