Title: Sidebar post filter
Last modified: August 20, 2016

---

# Sidebar post filter

 *  [eian00](https://wordpress.org/support/users/eian00/)
 * (@eian00)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/sidebar-post-filter/)
 * 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

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [hotplugin](https://wordpress.org/support/users/hotplugin/)
 * (@hotplugin)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/sidebar-post-filter/#post-2570481)
 * 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’);
 *  Thread Starter [eian00](https://wordpress.org/support/users/eian00/)
 * (@eian00)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/sidebar-post-filter/#post-2570603)
 * 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?

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Sidebar post filter’ is closed to new replies.

## Tags

 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [eian00](https://wordpress.org/support/users/eian00/)
 * Last activity: [14 years, 3 months ago](https://wordpress.org/support/topic/sidebar-post-filter/#post-2570603)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
