Can't use wp_query "meta_key" parameter to filter posts

Support MB Group Can't use wp_query "meta_key" parameter to filter posts

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1823
    billing@archcreativegroup.com[email protected]
    Participant

    I think my problem may be tangentially related to this post.
    https://support.metabox.io/topic/wp_query-with-meta_value-impossible-on-cloned-data-fields-array/

    I'm trying to filter posts that have a custom field created using the groups extension, based on the value of a meta key.

    The meta key I'm trying to use:
    'meta_key' => 'end_date_AUCTION_HERO',

    If I var_dump rwmb_meta($META_ID.'_text_block')
    I can see end_date_AUCTION_HERO is a valid key, so that's something.

    I know I can get access to the metabox.io custom field keys of post within a wp loop easily, but it doesn't seem possible to access them outside the loop, like when you need to make an instance of wp_query

    I could get it like this within a loop
    //LOOP
    $META_ID = 'AUCTION_HERO';
    $text_block = rwmb_meta($META_ID.'_text_block');
    echo $text_block['end_date_AUCTION_HERO_'.$META_ID]
    //END LOOP

    I can't do that outside the loop of course, because rwmb won't spit anything out.

    Is it possible to do this with groups, or alternatively, without groups?
    I've definitely done this task before when making custom fields from scratch, just seem to be having trouble with metabox.

    #1825
    Anh TranAnh Tran
    Keymaster

    Hi, as I understand, there are 2 problems here:

    1. Filtering WP_Query directly with meta values from group
    2. Get the meta values from group outside the loop and use them to filter the query

    Regarding the 1st problem: it's the same problem of sanitization data as in the link you provided. Data for a group is saved as a sanitized string of an array. So it's can't be used directly to filter the query.

    The 2nd issue seems the solution you made to filter the query, by getting the meta values first (and maybe compare them with a specific value). It's very simple to get the value outside the loop. All you need to do is providing the 3rd param for rwmb_meta function: the post ID, like this:

    //END LOOP
    $post_id = ''; // Set the post ID here
    $META_ID = 'AUCTION_HERO';
    $text_block = rwmb_meta($META_ID.'_text_block', '', $post_id );
    echo $text_block['end_date_AUCTION_HERO_'.$META_ID];
    #1828
    billing@archcreativegroup.com[email protected]
    Participant

    Thanks!

    I'm going to try this out, and get back to you.
    I also had the idea of not using wordpress' filtering logic, but manually do it myself within the loop, and not echo html unless the post passes my filter.

    Will reply soon.

    #1829
    billing@archcreativegroup.com[email protected]
    Participant

    Question though,

    I don't think it's possible to access a post's id outside the loop. Maybe I'm not understanding you though

    #1868
    Anh TranAnh Tran
    Keymaster

    I mean getting the ID of the post you want to get value for. Inside the loop, you setup global post object and the helper function auto takes its ID. But outside the loop, the helper function doesn't know which post object to take ID, and thus it might output the wrong value.

    I'm not sure if that's the case, cause the situation is not 100% clear. That are just my thoughts.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Can't use wp_query "meta_key" parameter to filter posts’ is closed to new replies.