0

I need a little more clarification on the default query for a specific custom post type url. I am learning how to use WP default functions and queries via Udemy. However, I feel like this little part I need either better clarification or confirmation from the community. In the video in Udemy, it uses the variable $query as an argument in a function in order to customize the default query for a custom post type. However, I do not see anywhere else in the training that shows where that variable comes from. Yet, somehow, we are still able to implement it as an object in order to look into it for other data points or to customize the default query for a custom post type.

    function university_adjust_queries($query) {

    if(!is_admin() && is_post_type_archive('program') && $query->is_main_query()){
        $query->set('orderby', 'title');
        $query->set('order','ASC');
        $query->set('post_per_page', -1);
    }

    if(!is_admin() AND is_post_type_archive('event') AND $query->is_main_query()){
        $today = date('Ymd');   
        $query->set('meta_key', 'event_date');
        $query->set('orderby', 'meta_value_num');
        $query->set('order','ASC');
        $query->set('meta_query', array(
            array(
              'key' => 'event_date',
              'compare' => '>=',
              'value' => $today,
              'type' => 'numeric'
            )
          ));
    }
}

add_action('pre_get_posts','university_adjust_queries');

Is this variable a default variable in the WordPress application that everyone is used to using or is it just something that was created on the fly?

If it is a default variable widely used by WP, then is this the key variable I can go to no matter what the custom post type or default post type is? Is it always stored in the same $query variable?

Is that the reason why the instructor decided to use it, although I can't see it anywhere else? Is it just common knowledge now?

Thank you

2
  • 1
    can you include the code that you're asking about? Not everybody is familiar with the Udemy courses, or what the instructor did, nor will many people watch the courses to find out what you're referring to, or spend the money to gain access to them. Use the Edit link under the list of tags to edit your question and include the missing context Commented Dec 1, 2022 at 14:07
  • @TomJNowell Thanks for the input and yes, I will go ahead and add the code. Commented Dec 1, 2022 at 15:59

1 Answer 1

1

The code you shared is for an action/filter named pre_get_posts. WordPress will run this action before it uses the arguments for WP_Query objects to generate SQL. It is an opportunity for you to modify that queries arguments/parameters. By doing this it will call all the functions attached to that action/filter/event.

Is this variable a default variable in the WordPress application that everyone is used to using or is it just something that was created on the fly?

No. You can see where the variable came from, it's a function parameter: ...ries($query) {. This isn't a WordPress thing, this is a basic programming thing.

Take this example:

function foo( $bar ) {
    echo $bar;
}
$banana = 'hello';
foo( $banana );

Where does $bar come from? It is not a global, it's just the name of the first argument for the function named $foo. You do not need to know how WordPress calls the function, just that it gets called.

If it is a default variable widely used by WP, then is this the key variable I can go to no matter what the custom post type or default post type is? Is it always stored in the same $query variable?

No, it isn't. The reason it's called $query is because that's the name of the first function argument. If you renamed it to $oranges it would be called $oranges, there is nothing special about the choice of name. It's probably called $query because it's a query.

This code is just as valid:

function university_adjust_queries( WP_Query $university_query ) {

    if(!is_admin() && is_post_type_archive('program') && $university_query->is_main_query()){

So is this:

function university_adjust_queries( WP_Query $kittens ) {

    if(!is_admin() && is_post_type_archive('program') && $kittens->is_main_query()){

But you wouldn't use kittens as a name because it's not very readable and gets confusing.

Is that the reason why the instructor decided to use it, although I can't see it anywhere else? Is it just common knowledge now?

You wouldn't use the functions argument variables outside the function, that makes no sense. Take this example:

function foo( $bar ) {
    echo $bar;
}
foo( "banana" );
echo $bar; // <- this doesn't work, we are not inside the foo function so $bar doesn't exist here, it isn't the same variable

This is now touching on the programming concept of "scope". Think of it like unconnected conversations, the $bar inside the function and the $bar outside the function are not the same variable, they just happen to have the same name, and have no other connection. There is also nothing special about the choice of name, $veranda or $purple are equally valid choices.

1
  • 1
    Thank you so much! That was very well explained. It connected many dots for me. Commented Dec 13, 2022 at 18:17

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.