0

Hi I use a loop like this:

// show all active coupons for this store and setup pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array(
    'post_type' => APP_POST_TYPE,
    'post_status' => 'publish',
    APP_TAX_STORE => $term->slug,
    'ignore_sticky_posts' => 1,
    'posts_per_page' => -1,
    'paged' => $paged
) );

Now I have a meta value meta_key' => 'clpr_excoupon' it is either 1 or 0.

I would like to exclude all meta_key' => 'clpr_excoupon' with 'meta_value'=> 0, and also 'NOT EXISTS' need help!!

1

Do NOT use query_posts() - it is bad, very bad to be exact.

For more information read:

Use WP_Query instead.

Example with meta_key, meta_value and meta_compare parameters in use:

$args = array(
    'meta_key'     => 'clpr_excoupon',
    'meta_value'   => '0',
    'meta_compare' => '!='
);
$query = new WP_Query( $args );

Note: those parameters work for query_posts too, but as said, you shouldn't use it.


Update:

With NOT EXISTS compare.

$args = array(
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'clpr_excoupon',
            'compare' => 'NOT EXISTS',
            'value'   => 'prior to WP 3.9 a value was needed due to bug #23268'
        ),
        array(
            'key'     => 'clpr_excoupon',
            'compare' => '!=',
            'value'   => '0'
        ),
    ),
);
$query = new WP_Query( $args );
  • thanks for your answer however I cant get it to work, i have other loops on the same page with query_posts(). How do i exchange the query_posts() for WP_Query? – Ingo Meinhard May 22 '15 at 20:21
  • I really can't account for you making the wrong specification, right? Aside from that the answer is correct. @IngoMeinhard – Nicolai May 23 '15 at 8:00
  • somebody is having a bad day, i was not complaining and thank you for pushing me in the right direction ..... – Ingo Meinhard May 23 '15 at 12:58
  • Sorry, if it came across that way, but all is good on my side. Believe me, you don't want to get in my way, if I am really having a bad day. @IngoMeinhard – Nicolai May 23 '15 at 20:02
-1

You should use 2 queries for this:

$excluded = query_posts( array(
        'meta_key=> 'clpr_excoupon';
        'meta_value' => '0'
) );

//$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array(
    'post__not_in => $excluded;
    'post_type' => APP_POST_TYPE,
    'post_status' => 'publish',
    APP_TAX_STORE => $term->slug,
    'ignore_sticky_posts' => 1,
    'posts_per_page' => -1
    //'paged' => $paged
) );

with this code you will get all the posts that have the clpr_excoupon with value 1 :)

Hope this helps...

Cheers, Mile

  • -1 for using query_posts, and you have obvious syntax errors in your code – Pieter Goosen May 23 '15 at 13:11
  • -1 because i want to show alle post without pagination – Ingo Meinhard May 23 '15 at 13:41
-2

It is working with this, thanks @ialocin for your help :

// show all active coupons for this store and setup pagination
                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    query_posts( array(
                        'post_type' => APP_POST_TYPE,
                        'post_status' => 'publish',
                        APP_TAX_STORE => $term->slug,
                        'ignore_sticky_posts' => 1,
                        'posts_per_page' => -1,
                        'meta_query' => array(
    'relation' => 'OR',
    array(
        'key'     => 'clpr_excoupon',
        'compare' => 'NOT EXISTS'

    ),
    array(
        'key'     => 'clpr_excoupon',
        'compare' => '!=',
        'value'   => '1'
    ),
),
                        'paged' => $paged
                    ) );
  • strange i needed 1 to not show it???? – Ingo Meinhard May 22 '15 at 21:06
  • -1 for using query_posts – Pieter Goosen May 23 '15 at 7:18
  • well its in my theme and i do not want to change the hole theme. Instead of -1 you could explain how to change to WP_Query – Ingo Meinhard May 23 '15 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.