Support » Fixing WordPress » query or pre get post?

  • I’ve got a set of custom taxonomies set up for “home-location” where a person lists where they live. I have set up a “visiting” category as well. A user can set tour dates and the category will change to and from the visiting location on those dates automatically.

    For the “home-location” archive page, I have a 3 column grid set up that shows the individual’s featured image and the custom post title all styled fine with CSS. It labels the post “filed under: ‘home-location'” which I also like.

    What I want to do is include the individuals that are visiting within the grid. I’m still new to php and am struggling with the loop. I’m trying to understand running a query to get two categories. In reading the documentation, it looks like pre_get_posts might be a better option?

    It looks like it adds to the existing variables instead of overriding the original loop. Of course I could be totally wrong there…

    Let’s say the “home-location” has the slug: los-angeles and cat: 5. The “visiting-location” has slug: visiting-los-angeles and cat: 6. Here is the relevant code for the page. Any suggestions on how to best achieve integrating the two?

    <div id="primary" class="site-content">
    	<div id="content" role="main">
    		<header class="page-header">
    			<h1 class="page-title">
    					printf( __( 'Filed Under: %s' ), '<span>' . single_cat_title( '', false ) . '</span>' );
    		// puts posts in a 3 column grid
    		$col = 0;
    		$col_count = 3;
    		$cols = array();
    		while (have_posts()) : the_post();
    		if($col >= $col_count) $col = 0;
    	<div class="post" id="post-'<?php the_ID(); ?>'">
    		<h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
    			<div class="entry">
    					if ( has_post_thumbnail() ) {the_post_thumbnail( 'index-thumbnail' );
    					// the current post has a thumbnail
    					} else {
    					// the current post lacks a thumbnail
    		$output = ob_get_contents();
    		$cols[$col++] .= $output;
        <div class="columns">
    			foreach($cols as $key => $col)
    			echo '<div class="column column' . $key . '">' . $col . '</div>';
  • The topic ‘query or pre get post?’ is closed to new replies.