• Hello everyone,

    I guess it’s just a general question but I don’t understand what is the idea behind calling functions within the class wp_query inside normal functions. For example, the function have_posts calls a function that is inside the class wp_query. Why use a function inside a function?

    Thanks in advance,
    Omer

Viewing 1 replies (of 1 total)
  • Pretty much every WordPress template needs to use the have_posts() and the_post() functions of the main query to work, so rather than making developers do this:

    global $wp_query;
    
    while ( $wp_query->have_posts() ) : $wp_query->the_post();
    
    endwhile;
    

    The have_posts() and the_post() functions are made available as shorter ways to deal with the main query:

    while ( have_posts() ) : the_post();
    
    endwhile;
    

    It’s sort of a form of ‘syntactic sugar’, just to make development a bit easier.

Viewing 1 replies (of 1 total)
  • The topic ‘wp_query class’ is closed to new replies.