0

I am displaying a set of upcoming fixtures for two cricket teams on the same page. The fixtures are created from a CPT and the relevant team is selected from a custom field.

I have everything working as I would want but, I have custom loops set up and the only difference between the two is one key => value pair.

 $firstXIFixtures = new WP_Query(array( 'posts_per_page' => -1, 'post_type' => 'fixture', 'orderby' => 'meta_value_num', 'meta_key' => 'date', 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'team', 'compare' => '==', 'value' => '1st_XI', 'type' => 'text' ), ), )); 

I only need to change 'value' => '1st_XI' to 'value' => '2nd_XI' in the next loop.

If it helps to give context, the fixtures are in a two column layout with separate headings above to indicate which teams the below fixtures are for.

enter image description here

How do I make this template file a little cleaner as it feels quite repetitive?

    1 Answer 1

    1

    You could define an array $fixture = array ('1st_XI','2nd_XI') and loop through that, like this:

    foreach ($fixture as $value) { $firstXIFixtures = new WP_Query(array( ... 'value' => $value, ...)); ... the html you use to display the column } 
    2
    • thank you for your feedback. although I didn't use this solution in the end, it prompted me to think differently and I have now wrapped the custom query in a function that has the parameter $team. I have tried to find other examples of custom queries being used like this and didn't find much so I am left wondering why...CommentedOct 20, 2024 at 18:47
    • I don't understand why you don't use cjbj's proposition ? what is "it" in "it prompted me to think differently" ?
      – mmm
      CommentedOct 21, 2024 at 9:47

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.