Support » Fixing WordPress » Post Loop to show only posts with meta property

  • Hello.
    I am very new to WordPress so be easy on me.
    I am using the JigoShop e-commerce plugin (which I think is irrelevant to this issue but I thought I should mention it).

    My posts, show each of the products on my site. Each post has some additional meta properties one of which is the stock level ‘stock.

    What I want to do is to modify my post loop so that it only shows items with sock.

    I started off with (basically) this:

    if (have_posts()) : while (have_posts()) : the_post(); 
            $stockLevel = get_post_meta($post->ID, 'stock', true);
    	if ($stockLevel > 0)
                  ...display post
    endwhile; else :

    Now this sort of works as it shows only my posts that have a stock level > 0, but this means I don’t get a full list of items. For instance, if I have it set up to show 10 items per page and 9 have 0 stock, then I will only but showing 1 item on this page.

    So the real question is, how do I wrap the meta property stock check up in a query that I can then display posts from?

    Many thanks.

Viewing 10 replies - 1 through 10 (of 10 total)
  • I think it will work to place this just before the if (have_posts()) line:

    global $wp_query;
          array('meta_key' => 'stock',
                'meta_value' => 0,
                'meta_compare' => '>')

    Cheers for that.
    I think I see what it’s doing.
    I’ll give it a try in a while and post how it goes.

    I had quick go at this in-between the kids waking throughout the night and I couldn’t get it working. It seemed to select a load of correct posts but also a load of invalid posts. Not quite sure what they were as the data within them seemed completely invalid.

    I think what you posted is right as it matches what a lot of searches were bringing up. I think I am doing something else wrong.

    Cheers, anyway.

    Hmmm… this is very odd.

    In my main loop if I put the following code:
    echo 'instock='.get_post_meta($post->ID, 'stock', true).'. ';

    I get output alongside my items specifying their stock levels (only 0 or 1 at the mo). Checking these values they are all correct.

    However if I add the following before the loop:

    I get no posts shown at all. I have tried multiple variants of this query but without any luck.

    Any idea why this is failing even though the get_post_meta proves the data is there?

    Just in case it is useful, this is what my loop looks like now:

    // The new line that breaks everything when it is supposed to show
    // posts with >= 0 stock (should be 1, but 0 for testing)
    if (have_posts()) : while (have_posts()) : the_post(); 
    	$_product = &new jigoshop_product( $post->ID );
            // The line that proves the meta data is there
    	echo 'instock='.get_post_meta($post->ID, 'stock', true);
    endwhile; endif;


    I can’t say for sure, but it may be that you can’t use the query-string for all these paramters. Try using the array form instead:

    $args = array(
       'meta_key' => 'stock',
       'meta_compare' => '>=',
       'meta_value' => 0,
       'posts_per_page' => 100,

    Cheers for the support.
    If my kids let me I’ll test that out tonight.

    Nope. Returned nothing with that.
    When I edited it to just a NULL array like:

    $args = array();

    I got all the items as you would expect (but I think it proves the query_post with an array worked).

    However just making a simple array such as:

    $args = array('meta_key' => 'stock');


    $args = array('posts_per_page' => 100);

    both give no results.

    Ok, little more work and I have done the following:

    global $post;
    $args = array('numberposts' => 3);
    $custom_posts = get_posts($args);
    foreach($custom_posts as $post) : setup_postdata($post);
    	echo 'GOTPOSTS';
    $first_query = new WP_Query('posts_per_page=3');
    while($first_query->have_posts()) : $first_query->the_post();
    	echo 'WPQUERY';

    Now I would expect one of those to output something, but neither do.
    I think there is something fundamentally wrong going on here.

    I am going to try to see if I can get any help from Jigoshop on this, but I doubt it now they want you to pay for support (and the main reason I went with them was they were free!)

    I am beginning to suspect that there are filters in place to modify queries. I will be glad to take a look at the code if you are willing to send me a zipped copy of the theme (provided that does not violate any license).

    Please use this Contact Me page to get in touch and include a reference to this thread.

    Cheers for the offer. It looks like it was something purely down to the plugin. They somehow specify / override what the query was (maybe with filters. Didn’t really understand it) in a separate file and making my tweaks there got it working.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Post Loop to show only posts with meta property’ is closed to new replies.