I’m working on some shortcodes to display posts in a certain way so that the output can be copy/pasted into Mailchimp for a weekly newsletter. These are all standard WordPress posts, but there are Advanced Custom Fields involved in selecting the post.
Basically, we are using ACF fields to identify:
* Posts about 1-time events – ACFs are start\_datetime and end\_time
* Posts about recurring events – ACFs are start\_datetime, end\_time, end\_date, and recurrence (a description like “4 Mondays from 4 – 6pm” – yes, this could be extrapolated from the other ACFs, but it’s easier just to ask people to provide a description)
* Posts that are just announcements, unrelated to any specific dated events – we generally want these to run in the newsletter for no more than 2 consecutive weeks, so we use the post\_date as a cutoff (newsletter publication date minus 14 days)
We are also using some calculated dates based on the publication date of the next newsletter so that we don’t pull in posts about one-off events that will have already started by the time the newsletter is published or recurring events that will have ended before the newsletter publication date, etc.
I have shortcodes working for all of these needs, but something else has come up with the announcement-only posts. The follow code is working fine for that – we set a value for $earliest\_date (2 weeks before the next newsletter publication date), make sure the post\_date is after that, and make sure that the start\_datetime ACF is empty or doesn’t exist so we know it’s not a post about a dated event. (Note that it’s using date\_query to check the post\_date.)
`$uucp_args = array(`
`’category_name’ => $atts[‘category’],`
`’posts_per_page’ => $atts[‘nr_posts’],`
`’date_query’ => array(`
`array(`
`’after’ => $earliest_date,`
`’column’ => ‘post_date’,`
`),`
`),`
`’meta_query’ => array(`
`’relation’ => ‘OR’,`
`array(`
`’key’ => ‘start_datetime’,`
`’compare’ => ‘NOT EXISTS’`
`),`
`array(`
`’key’ => ‘start_datetime’,`
`’value’ => ”,`
`’compare’ => ‘=’`
`)`
`),`
`’orderby’ => ‘date’,`
`’order’ => ‘DESC’`
`);`
But then we realized that there are some special cases in which we’d really like to let a general announcement hang around longer than 2 weeks. Obviously, we could go in and update the publication date, but that’s kind of kludgy.
So I thought we could add another ACF called expiration\_date and use that if we wanted to extend the life of a post. The shortcode would check the post\_date against the $earliest\_date value, but it would also check to see if there was an expiration\_date that was after the publication date.
`$query_args = array(`
`’category_name’ => $atts[‘category’],`
`’posts_per_page’ => $atts[‘nr_posts’],`
`’meta_query’ => array(`
`’relation’ => ‘OR’,`
`array(`
`array(`
`’relation’ => ‘OR’,`
`array(`
`’key’ => ‘start_datetime’,`
`’compare’ => ‘NOT EXISTS’`
`),`
`array(`
`’key’ => ‘start_datetime’,`
`’value’ => ”,`
`’compare’ => ‘=’`
`)`
`),`
`array(`
`’key’ => ‘post_date’,`
`’value’ => $earliest_date,`
`’compare’ => ‘>=’,`
`)`
`),`
`array(`
`’key’ => ‘expiration_date’,`
`’value’ => $pub_date,`
`’compare’ => ‘>=’`
`),`
`),`
`’orderby’ => ‘date’,`
`’order’ => ‘DESC’,`
`);`
**But this is only pulling in posts that have an expiration\_date.** I want to pull in everything that the first query pulls in plus anything with an expiration\_date that’s after the next publication date. I have tried umpteen gazillion variants, trying to get something to work, but I’m clearly missing something.
Any suggestions?
[ad_2]
Yeah the problem is by adding an ACF later down the line, all previous posts don’t have the post meta for it so using it as a qualifier in your `WP_Query` would exclude those older posts.
Could you run the two queries and then merge & sort the two result arrays?