Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • OK, I figured out how to solve this a bit more elegantly. Add these codes to wp-content/themes/[theme-directory]/functions.php

    add_filter('query_vars', 'metakey_queryvars' );
    function metakey_queryvars( $qvars )
    {
      $qvars[] = 'not_meta_key';
      return $qvars;
    }
    
    add_filter('posts_where', 'metakey_where' );
    function metakey_where( $where )
    {
        global $wp_query;
        global $wpdb;
    
        if( isset( $wp_query->query_vars['not_meta_key'] )) {
           $where .= $wpdb->prepare(" AND $wpdb->posts.ID NOT IN ( SELECT post_id FROM $wpdb->postmeta WHERE ($wpdb->postmeta.post_id = $wpdb->posts.ID) AND meta_key = %s) ", $wp_query->query_vars['not_meta_key']);
        }
    
        return $where;
    }

    What the above codes do is adding a custom parameter called ‘not_meta_key’. So now you can do this:

    $myposts = get_posts('category=3&not_meta_key=feature-sidebar');

    Sorry to hijack this thread, but I have a similar problem. So I’m wondering if anyone has a clean solution to this problem?

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