Custom post query but get only posts with IF statement
-
Hello,
I will try to explain my issue here as much as I can, please let me know if it makes sense. I have searched for an answer to this but couldn’t get it.So, I have a custom post type called “race”. Every race has custom fields, let’s call them “A”, “B”, “C”.
“A” can be an image field, and “B” a checkbox list.
What I want to do is to query and get / list only “races” that have a value on “A”. So if “A” is an image, I can show a list of “races that have image”. And if “B” can take values “HM”, “FM”, “5km”, I can show only “races which are HM”.
I have managed to do that by
<?php if( get_field('A'): ?>
andif( in_array( 'FM', get_field('race_category') ) or 'FM' == get_field('race_category') ): ?>
respectively, the display works BUT, the query still gets all “race” posts, even if it only displays the ones which satisfy the criterion. I know this because of pagination.For example, I have a total of 16 “race” entries, but only one with “FM” category. As you see at the link, I manage to get it, but it is displayed on the 4th page (current pagination is 5 posts / page, and this is alphabetically the last entry, so pagination works). https://www.justrunlah.com/blog/sg-races-db-full-marathon-events/
Also, I know how to use args arrays for simple stuff, for example I use the following to get entries that are within 2014 only, but I don’t know how to implement the more complicated statements needed into the args code.
<?php global $paged; $curpage = $paged ? $paged : 1; $today = date('Ymd'); $args = array( 'post_type' => 'race', 'orderby' => 'race_date', 'meta_key' => 'race_date', 'order' => 'DES', 'posts_per_page' => 50, 'paged' => $paged, 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'race_date', 'compare' => '<=', 'value' => 20141231, ), array( 'key' => 'race_date', 'compare' => '>=', 'value' => 20140101, ) ), ); ?>
In the following PasteBin I am giving you the code that gives that issue: https://pastebin.com/8hwdw0us
Thanks in advance
- The topic ‘Custom post query but get only posts with IF statement’ is closed to new replies.