Can't use wp_query "meta_key" parameter to filter posts
- This topic has 4 replies, 2 voices, and was last updated 9 years, 4 months ago by
Anh Tran.
-
AuthorPosts
-
November 29, 2015 at 10:50 PM #1823
[email protected]
ParticipantI 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 LOOPI 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.November 30, 2015 at 2:14 PM #1825Anh Tran
KeymasterHi, 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 queryRegarding 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];
November 30, 2015 at 11:11 PM #1828[email protected]
ParticipantThanks!
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.
November 30, 2015 at 11:15 PM #1829[email protected]
ParticipantQuestion though,
I don't think it's possible to access a post's id outside the loop. Maybe I'm not understanding you though
December 5, 2015 at 3:42 PM #1868Anh Tran
KeymasterI 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.
-
AuthorPosts
- The topic ‘Can't use wp_query "meta_key" parameter to filter posts’ is closed to new replies.