Forums

meta_query using custom field not working? (6 posts)

  1. ixwa
    Member
    Posted 11 months ago #

    I have practically identical queries like this on other pages that work just fine and I cannot figure out why this one doesn't return any results.

    $args = array(
    	'category_name' => 'reviews',
    	'meta_query' => array(
            array(
    				'key' => 'product_id',
    				'value' => $post->ID,
    				'compare' => 'LIKE'
    				)
    	),
    	'orderby' => 'post_date',
    	'order' => 'DESC',
    	'showposts' => '2'
    );
    
    $wp_query = new WP_Query( $args );
    etc.

    This query returns results when I _exclude_ the meta_query which is bizarre since posts DO exist for category and the correct value exists in the custom field (I'm using compare -> LIKE here just as a catch-all).

    Have I missed something??

  2. ixwa
    Member
    Posted 11 months ago #

    Not solved yet but i did discover something utterly weird: Using $post->ID or $postId doesn't work, but using a literal number like '825' works.

    Even stranger: if I use $_GET[p] (from the URL query string) it works.

    What in God's name is going on? Obviously there's something bad about using WP-specific vars. (I run other wp_querys on this page before this one, but have tried resetting postdata or the wp_query and neither affects it.)

  3. esmi
    Theme Diva & Forum Moderator
    Posted 11 months ago #

    Try something like $postid = $post->ID; and then dropping the $postid variable into your arguments.

  4. ixwa
    Member
    Posted 11 months ago #

    Yes, I mentioned that I had tried this. Nada.

    It's like it can't identify this $post->ID within the $args() array even though echoing it is successful at any point. Is there some other method for using a global var within an array? (Even though I've done this successfully elsewhere)

  5. esmi
    Theme Diva & Forum Moderator
    Posted 11 months ago #

    How are you grabbing $post?

  6. ixwa
    Member
    Posted 11 months ago #

    Earlier in the page I have a $wp_query (on a different post_type) but even before this I'm loading up generic get_post_meta without running any direct queries at all (works fine).

    $post->ID echoes a value outside of this, just not within $args. And the fact that I can't even assign this value to a different local var is puzzling.

Reply

You must log in to post.

About this Topic