1

I want to use the following code multiple times in the same page template, however the template gets too big when it comes to lines of code and therefore size. Every time I use it I change only one parameter and 99% remains the same. The parameter I change is 'terms'=>'pop'. I know I can display all posts and all taxonomy terms (taxonomies posts under each taxonomy) with a relatively small piece of code but that will not work as I want since I want to order taxonomies in a custom way, not by title or term (ASC/DSC). What I am thinking is maybe add the code to a fuction and each time I call the function I just pass the parameter which changes.

The code:

$query = new WP_Query( array(
                        'post_type'      => 'stations',
                        'post_status' => 'publish',
                        'posts_per_page' => -1,
                        'orderby' => 'title',
                        'order' => 'ASC',
                        'tax_query' => array(
                                         array(
                                            'taxonomy' => 'genres',
                                            'field' => 'name',
                                            'terms' => 'pop' // THIS IS WHAT CHANGES
                                             )
                                       )
                     ) );
5
  • You answered your own question already: write a function with the variable query argument as a parameter. So... what's your question?
    – bosco
    Commented Feb 2, 2016 at 20:37
  • Or, to reduce queries, you could just grab all of the terms at once and sort them after they're retrieved.
    – bosco
    Commented Feb 2, 2016 at 20:39
  • Problem is I do not know how to write that function.
    – Arg Geo
    Commented Feb 2, 2016 at 20:44
  • Your question may be off-topic for pertaining specifically to PHP. We also ask that questions display research effort - take a look over the "How to Ask" page in the help center. You can find your answer by reviewing PHP's documentation on user-defined functions, or Googling "PHP function" should bring up a number of tutorials.
    – bosco
    Commented Feb 2, 2016 at 20:46
  • Sorry for that but I always research before asking here. Waiting for answers while I can find something really quick is not what I like.
    – Arg Geo
    Commented Feb 2, 2016 at 21:00

2 Answers 2

1

To expand on the answer by Global:

Paste this into your theme's functions.php:

function get_stations_query( $terms ) {
    //create new query object
    $query = new WP_Query( array(
                    'post_type'      => 'stations',
                    'post_status' => 'publish',
                    'posts_per_page' => -1,
                    'orderby' => 'title',
                    'order' => 'ASC',
                    'tax_query' => array(
                                     array(
                                        'taxonomy' => 'genres',
                                        'field' => 'name',
                                        'terms' => $terms
                                         )
                                     )
                 ) );
    return $query; //return the object
}

Then in your template files you can use:

$query = get_stations_query('pop');
1
  • Thank you. I did something like this already and it works. It does not require return $query; and I call it like <php? get_stations_query('pop'); ?>. Rest of the code is like yours. Is there anything wrong with this even though it works?
    – Arg Geo
    Commented Feb 2, 2016 at 21:44
0

Create a new function, put all the code in it and replace 'pop' with $genre. Then call it like this:

get_stations_query( $genre );

the return of your function should be

return $query;
2
  • To clarify, you call it like: get_stations_query( 'pop' );
    – LukeWarm
    Commented Feb 2, 2016 at 20:39
  • Can you give an example?
    – Arg Geo
    Commented Feb 2, 2016 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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