• Resolved Giorgio25b

    (@giorgio25b)


    The question today is this with customizr:

    If I modify the loop in a page template using new WP_Query

    <?php $args = array(
            'post_type' => 'projects',
            'posts_per_page'    =>  -1,
            'cat' => 77
        );
    $query = new WP_Query( $args); ?>
    
                            <?php if ( $query->have_posts() ) : ?>
                                <?php while ( $query->have_posts() ) : ##all other cases for single and lists: post, custom post type, page, archives, search, 404 ?>
                                    <?php $query->the_post(); ?>
                                    <?php do_action ('__before_article') ?>
                                        <article <?php tc__f('__article_selectors') ?>>
                                            <?php do_action( '__loop' ); ?>
                                        </article>
                                    <?php do_action ('__after_article') ?>
                                <?php endwhile; ?>
                            <?php endif; ##end if have posts ?>
    
    <!--  Reset Query -->
    <?php wp_reset_query(); ?>

    I get the list of what I want but no links to the title, entry metas and so on;

    but if I use query_posts instead of new WP_Query

    <?php query_posts(
        'post_type=projects&cat=77&posts_per_page=-1' );
    ?>
    
    // the customizr loop

    … the result is the same but with links and metas.

    WP is pushing the use of new WP_Query instead of query_posts, but query_posts actually works better than new WP_Query in the customizr template.

    Am I missing something here? Did I get it wrong?
    Any tip on this please?

Viewing 7 replies - 1 through 7 (of 7 total)
  • See here: https://themesandco.com/snippet/three-techniques-to-alter-the-query-in-wordpress/
    look at the $wp_the_query way
    this way you can have the new query in $wp_query, and use just have_posts() and the_post() and at the end of your loop reassign $wp_the_query to $wp_query.

    Thread Starter Giorgio25b

    (@giorgio25b)

    @d4z_c0nf
    Many thanks! I guess I was missing the most important part:
    reassigning $wp_the_query to $wp_query

    I have only one question (though I’m marking this as resolved) in the snippet 3 techniques to alter the query, it is presented as a function: am I able to parse it in the functions.php? bec I tried, but it did not work…

    This is the working loop I put in my custom-page-template:

    <?php do_action( '__before_loop' );##hooks the header of the list of post : archive, search... ?>
    
                        <!-- custom loop -->
                        <?php
                            global $wp_query, $wp_the_query;
    
                            $wp_query = new WP_Query( array(
                                'post_type'         => 'post',
                                'post_status'       => 'publish',
                                'posts_per_page'    => 5,   //show n posts
                                'cat'               => -35, //exclude this category from the posts list
                               //others parameters here: https://codex.www.ads-software.com/Class_Reference/WP_Query#Parameters
                            ) );
                        ?>
    
                            <?php if ( have_posts() ) : ?>
                                <?php while ( have_posts() ) : ##all other cases for single and lists: post, custom post type, page, archives, search, 404 ?>
                                    <?php the_post(); ?>
                                    <?php do_action ('__before_article') ?>
                                        <article <?php tc__f('__article_selectors') ?>>
                                            <?php do_action( '__loop' ); ?>
                                        </article>
                                    <?php do_action ('__after_article') ?>
                                <?php endwhile; ?>
                            <?php endif; ##end if have posts ?>
    
       <?php $wp_query = $wp_the_query; ?>
       <!--  Reset Query -->
       <?php wp_reset_query(); ?> 
    
    <?php do_action( '__after_loop' );##hook of the comments and the posts navigation with priorities 10 and 20 ?>

    Hi Giorgio25b
    well, first thing you don’t have to reset the query if you use the wp_the_query method.
    Anyway don’t know why you want to port it in functions.php . I think it’s better having it in a custom template, this way checks on conditional tags will not be performed every time you load a page.
    You can make a custom template this:

    <?php
    /*
    *
    Template Name: Mah
    */
    
    add_action( '__before_loop'     , 'set_my_query' );
    add_action( '__after_loop'      , 'set_my_query', 100 );
    
    function set_my_query() {
        global $wp_query, $wp_the_query;
        switch ( current_filter() ) {
         case '__before_loop':
         //replace the current query by a custom query
        //Note : the initial query is stored in another global named $wp_the_query
        $wp_query = new WP_Query( array(
          'post_type'         => 'post',
          'post_status'       => 'publish',
          'posts_per_page'    => 5,   //show n posts
          'cat'               => -35, //exclude this category from the posts list
        ) );
         break;
    
         default:
         //back to the initial WP query stored in $wp_the_query
         $wp_query = $wp_the_query;
         break;
        }
    }
    /*
     * let's include customizr index.php
     * this way we'll not lose eventual
     * changes on it
     */
    include_once( TC_BASE . 'index.php' );

    Thread Starter Giorgio25b

    (@giorgio25b)

    Many thanks for the clarification and also for the tip on wp_reset_query; I had it because I’m listing 2 different loops.
    I tried my custom page with 2 loops without resetting the query and it works fine.

    Your last snippet answered my question! I was not able to understand why on the snippet section [/snippet/three-techniques-to-alter-the-query] is presented as a function; so I kind of got confused.

    It works great and all your precious information made the picture way more clear than ever.

    Way to go!

    Glad to help. ??

    Thread Starter Giorgio25b

    (@giorgio25b)

    Follow up on this topic in case somebody else is interested or had problems with pagination:

    after solving a related problem in this other pagination support tread, here below 2 updated working snippets for custom loops in custom page templates.

    Slimmer version, by @d4z_c0nf

    <?php
    /*
    Template Name: News-Events Page
    */
    
    add_action( '__before_loop'     , 'set_my_query' );
    add_action( '__after_loop'      , 'set_my_query', 100 );
    
    function set_my_query() {
        global $wp_query, $wp_the_query;
        switch ( current_filter() ) {
         case '__before_loop':
         //replace the current query by a custom query
        //Note : the initial query is stored in another global named $wp_the_query
        $wp_query = new WP_Query( array(
          'paged'             => get_query_var('paged') ? get_query_var('paged') : 1,
          'post_type'         => 'post',
          'post_status'       => 'publish',
          'posts_per_page'    => 2,   //show n posts
          'cat'               => -35 //exclude this category from the posts list
        ) );
         break;
    
         default:
         //back to the initial WP query stored in $wp_the_query
         //a.k.a. reset the main query
         $wp_query = $wp_the_query;
         break;
        }
    }
    /*
     * let's include customizr index.php
     * this way we'll not lose eventual
     * changes on it
     */
    include_once( TC_BASE . 'index.php' );
    
    //code nicely implemented by @d4z_c0nf
    Thread Starter Giorgio25b

    (@giorgio25b)

    Full template page:
    works with Customizr 3.2.17 and Customizr-Pro 1.0.11

    <?php
    /*
    Template Name: News-Events Page
    */
    ?>
    <?php do_action( '__before_main_wrapper' ); ##hook of the header with get_header ?>
    <div id="main-wrapper" class="<?php echo esc_attr( tc__f( 'tc_main_wrapper_classes' , 'container' ) ) ?>">
    
        <?php do_action( '__before_main_container' ); ##hook of the featured page (priority 10) and breadcrumb (priority 20)...and whatever you need! ?>
    
        <div class="container" role="main">
            <div class="<?php echo esc_attr( tc__f( 'tc_column_content_wrapper_classes' , 'row column-content-wrapper' ) ) ?>">
    
                <?php do_action( '__before_article_container' ); ##hook of left sidebar?>
    
                    <div id="content" class="<?php echo esc_attr( tc__f( '__screen_layout' , tc__f( '__ID' ) , 'class' ) ) ?> article-container">
    
                        <?php do_action( '__before_loop' );##hooks the header of the list of post : archive, search... ?>
    
                            <!-- custom loop -->
                            <?php
                                global $wp_query, $wp_the_query;
    
                                $wp_query = new WP_Query( array(
                                    'paged'             => get_query_var('paged') ? get_query_var('paged') : 1,
                                    'post_type'         => 'post',
                                    'post_status'       => 'publish',
                                    'posts_per_page'    => 2,   //show n posts
                                    'cat'               => -35 //exclude this category from the posts list
                                ) );
                            ?>
    <!-- code nicely implemented by @d4z_c0nf -->
    
                            <?php if ( have_posts() ) : ?>  
    
                                <?php while ( have_posts() ) : ##all other cases for single and lists: post, custom post type, page, archives, search, 404 ?>
    
                                    <?php the_post(); ?>
    
                                    <?php do_action( '__before_article' ) ?>
                                        <article <?php tc__f( '__article_selectors' ) ?>>
                                            <?php do_action( '__loop' ); ?>
                                        </article>
                                    <?php do_action( '__after_article' ) ?>
    
                                <?php endwhile; ?>
    
                            <?php endif; ##end if have posts ?>
    
                        <?php do_action( '__after_loop' );##hook of the comments and the posts navigation with priorities 10 and 20 ?>
    
                        <!-- reset the main query -->
                        <?php $wp_query = $wp_the_query; ?>
    
                    </div><!--.article-container -->
    
               <?php do_action( '__after_article_container' ); ##hook of left sidebar ?>
    
            </div><!--.row -->
        </div><!-- .container role: main -->
    
        <?php do_action( '__after_main_container' ); ?>
    
    </div><!--#main-wrapper"-->
    
    <?php do_action( '__after_main_wrapper' );##hook of the footer with get_get_footer ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘new WP_Query vs. query_posts in Customizr loop’ is closed to new replies.