Home>Support>Modify the Query for Loop using “pre_get_posts”

Modify the Query for Loop using “pre_get_posts”

Notice: This thread is over two years old; the information may be outdated. Please consider creating a new thread if you require free support. If you have an active SiteOrigin Premium license, you can email our premium support desk at [email protected].

Its been many weeks now I am trying to use the Vantage Theme along with PageBuilder to develop a website for eCommerce. because

1. most of the available eCommerce plugin do not work with Vantage, it breaks something or the other, when the complexity of the site increases or use of other plugin.

2. Many of features in the eComm Plugin is a overkill for my requirements.. or the Price of the plugin although reasonable but licence terms of per year or per site complicates life.

The progress till now has been a lot of pain but learning also, for I am noob with WP..

The support from the SiteOrigin Team has be good. although I wished they could provide advance support ( may be paid ). I hate one of the standard reply which I see “beyond … this is more of development / customisation”

for now I am releasing the following code, which i used to modify the query for the loop.

Vantage used the following hierarchy.
a) for custom type post Archive: Archive.php or archive-cpt.php and both of these php call loops/loop-grid.php through “get_template_part” which consist the loop to get the all the post.
b) for getting the Archive of Taxonomy ( custom category or Tags): archive.php if taxonomy.php does not exist,

We can create a template for each taxonomy { taxonomy-my_taxonomy.php } or more detailed for each terms of the taxonomy { taxonomy-my_taxonomy-my_term.php } and further create loop-my_term-grid.php and customise the loop.

For me the above is an outright pain, hence I prefer to use one template taxonomy.php and use the hook “pre_get_posts”. this workflow solve the following

I have 5 types of Custom Post Types( CPT) and many of them share same taxonomy. The issues is when the archive loop by default vantage theme pulls post from both or all the CPT. In this case the Product CPT shares Taxonomy ( its not clear in the code but let me explain Product CPT has a Taxonomy “Brand” and also Manufacture CPT also shares the Taxonomy “Brand”, hence when you want to show all post with Taxonomy “Brand” its shows Products of the brand instead of only brand.

Let me make it clear the following code is not the only method to go about this, “rewrite slug” on the Custom Post can also be used.

I have already taken more bal bal.. I shall post the details on my blog

/* to copied into function.php of your child theme */
function tsk_modify_query_of_loop( $query ) {
	// return for non archive 
	if ( ! is_admin() || ! is_home() || ! is_single() ) {
		if ( $query->is_main_query() &&  $query->is_archive() ) {
			// now get the tax
			$current_term = get_queried_object();
			// $taxonomy = get_taxonomy($current_term->taxonomy);
			// $taxName = $taxonomy->name;
			
			$taxName = $current_term->taxonomy;
			if ( $query->is_tax( $taxName ) ) {
			// if ($query->is_tax($taxName)) does not make sense as get_queried_object(); 
			// returns the taxonomy
			// but not sure, if the main_query may be for other loops..?
				
				$postType = array(
					'cameratypes',
					'va',
					'ipratingtypes',
					'applicationtypes',
					'cameraaccessories'
				);
				// taxonomy unique to the Custom Post Type Product, have to include 
				// "product" to list all post containing the unique taxonomy 
				if ( 'typeofproducts' == $taxName ) { $postType[] = 'product'; }
        		$terms = get_terms( $taxName , array( 'fields' => 'ids' ) );
        		$taxquery = array(
        			'relation' => 'OR',
        			array(
            			'taxonomy' => '$taxName',
            			'field' => 'id',
            			'terms' => '$terms',
            			'operator'=> 'NOT IN'
        			)
    			);
    			$query->set( 'post_type' , $postType );
        		$query->set( 'tax_query', $taxquery );
        	}
    	}
    }
    return $query;
}
add_filter('pre_get_posts', 'tsk_modify_query_of_loop');

your can email me for any clarification, due to spams, pls visit my blog and post a comment or submit through contact form

When the website is fully developed I shall post a TuT.. for now you can click the URL link issue URL to see the progress.

happy coding

Srikanth Kamath
http://tskamath.pactindia.net

URL: http://tsktech.in

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 10 years, 1 month ago Srikanth Kamath

    i have some request on the changed Grid of Loop-grid the code is a s follows

    /* copy the loop-grid from Vantage Theme Loops Folder into Loops folder in your Child Theme */
    /* edit the <!-- tsk edit --> */
    <?php
    /**
     * Loop Name: TSKEditedGrid
     */
    ?>
    <?php if( have_posts() ) : $i = 0; ?>
    	<div id="vantage-grid-loop" class="vantage-grid-loop">
    		<?php while( have_posts() ): the_post(); $i++; ?>
    			<article <?php post_class(array('grid-post')) ?>>
    			<!-- TSK Edit -->
                            <?php 				
    				$postType = get_post_type();
    				// var_dump( $postType );
    				// die();
    				$obj = get_post_type_object( $postType );	
    				$tax = $wp_query->queried_object->slug;
    				echo '<div class="tsk-archive-header">'. $obj->labels->singular_name .'</div>';
    			?> <!-- end TSK edit -->
    				<?php if( has_post_thumbnail() ) : ?>
    					<a class="grid-thumbnail" href="<?php the_permalink() ?>">
    						<?php the_post_thumbnail('vantage-grid-loop') ?>
    					</a>
    				<?php endif; ?>
    				<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
    				<div class="excerpt"><?php the_excerpt() ?></div>
    			</article>
    			<?php if($i % 4 == 0) : ?><div class="clear"></div><?php endif; ?>
    		<?php endwhile; ?>
    	</div>
    	<?php vantage_content_nav( 'nav-below' ); ?>
    <?php endif; ?>
    

    edit the Custom CSS or Edit the “style.css” in your child theme

    /* css style for Grid Loop */
    .tsk-archive-header {
         padding: 1px 1rem;
         background: hsl(0, 0%, 25%);
        font-size: 80%;
        text-transform: uppercase;
        color: #ffffff;
    }
    
Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More