1

I am developing a site for a client who is a Youth Football (Soccer) Club, with lots of different Teams depending on the abilities and ages of the children. I have added in a Custom Post Type called Match Reports so that the Managers of each Team can login and write posts for their latest matches their teams were involved in.

I also have a News section using the standard Posts part of WP. I have written a Shortcode which will display the News on the Homepage. What I want to be able to do is enable a system where one of the Match Reports can be added to the Homepage by selecting an option, using the Advanced Custom Fields plugin - it is a two-option radio button form, with true/false values - Show on Homepage? Yes/No.

In my News shortcode, I have added the Match Reports Custom Post Type in, which works perfectly, but I am not sure how to only show the Reports with the "Show on Homepage - Yes" radio button selected. Here are my Query Parameters:

$queryArgs = array(
    "post_type" => array( "post", "match-report" ),
    "meta_key" => "show_in_news",
    "meta_value" => "true"
);

The problem with this is that because the standard Posts do not have the Custom Field in, only the Match Reports show up, and if I remove the Custom Field queries, it will show all the Match Reports together with the Posts. I need it to only show the Match Reports with show_in_news (my Custom Field with radio buttons, created with Advanced Custom Fields, with a value of a true, together with all of the Posts.

Can anyone help me out? Thanks very much if so :)

  • @PieterGoosen thanks for the comment - if possible, could you give me an example? I'm not exactly sure how I'd go about doing that. Thanks :) – Tom Oakley Dec 22 '14 at 13:12
  • I will try to look into this a bit later. Posting from my phone, so it is a bit difficult. – Pieter Goosen Dec 22 '14 at 13:14
  • Ah, great, got it solved. Enjoy :-) – Pieter Goosen Dec 22 '14 at 14:00
1

Currently your meta query is exclusive and you need to make it inclusive. For example, you can save always the meta field show_in_news for match-report custom post type, even when the value is false. Then, you can include posts if custom show_in_news doesn't exist (standard posts) or if it exists and it is "true" for custom post type:

$queryArgs = array(
    "post_type" => array( "post", "match-report" ),
    "meta_query" => array( 
        "relation" => "OR",
        array(
            "key" => "show_in_news",
            "compare" => "NOT EXISTS",
        ),
        array(
            "key"   => "show_in_news",
            "value" => "true",
        ),
    ),
);

This should do the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.