Default WordPress Query for a Specific Custom Post Type URL

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.

tech