Title: Query Post Within Category and Offset
Last modified: August 18, 2016

---

# Query Post Within Category and Offset

 *  [Thad Allender](https://wordpress.org/support/users/endortrails/)
 * (@endortrails)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/)
 * I’m building a magazine theme and I’m relying heavily on category post queries
   to display the latest category specific posts on the main index. I have a “Main
   Feature” category where I post my latest entry, but that entry also shows up 
   in the category specific sections. Using the offset=1 variable offsets all categories
   by 1, which is undesirable.
 * Confused? Try looking at the page to better understand.
 * [http://www.thadallender.com](http://thadallender.com)

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/query-post-within-category-and-offset/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/query-post-within-category-and-offset/page/2/?output_format=md)

 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650700)
 * Let me see if I have this straight, before I see what you can do about it…
 * On the front page you display the latest post from several different categories,
   each under its own ‘heading’ (i.e. category) in separate queries.
 * You also have a Main Feature category, and — also — display the latest post from
   that on the front page in a separate query. But this Main Feature post may also
   be in one of the other categories displayed on the front page.
 * So what you want is to assure that only the appropriate category query is offset(
   by 1) when the Main Feature post also falls under that category.
 * Correct?
 *  Thread Starter [Thad Allender](https://wordpress.org/support/users/endortrails/)
 * (@endortrails)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650782)
 * You got it Kaf. Correct. Thoughts?
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650796)
 * I have a couple ideas on what could be done, but possibly the easiest solution
   is to use a custom variable assigned the category ID (or IDs if the Main Feature
   post can be set to two or more other front page categories) from the Main Feature
   post’s data. Then you only need test against each category query to decide if
   the offset must be set.
 * Here’s an example where only one additional category exists for the Main Feature
   post. First, somewhere in the Main Feature post loop:
 *     ```
       <?php
       $categories = wp_get_object_terms($post->ID, 'category');
       foreach( $categories as $category ) {
       	if ( $category->term_id != '100' ) {
       		$cat_offset = $category->term_id;
       	}
       }
       ?>
       ```
   
 * Modify `100` in `( $cat_id != '100' )` to the Main Feature category ID. If using
   category name in queries, change `$category->term_id` to `$category->name` (or`
   $category->slug` for the ‘sanitized’ version) and set the test string accordingly.
 * Next, we need the code appearing *before* the other queries:
 *     ```
       <?php
       $offset = 0;
       $categories = wp_get_object_terms($post->ID, 'category');
       foreach( $categories as $category ) {
       	if ( $category->term_id == $cat_offset ) {
       		$offset = 1;
       	}
       }
       ?>
       ```
   
 * Again, if necessary modify the associative array key (‘term_id’) on `$category-
   >term_id` to the appropriate one.
 * Finally, slip this in at the end of your query arguments:
 * `&offset=$offset`
 *  Thread Starter [Thad Allender](https://wordpress.org/support/users/endortrails/)
 * (@endortrails)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650806)
 * Great Kaf! I’m almost there, but I’m having trouble with the php syntax.
 * I would like to avoid duplicating Category 1, so based on your code above, would
   I put the following code above each query:
 *     ```
       <?php
       $offset = 0;
       $categories = wp_get_object_terms($post->ID, 'category');
       foreach( $categories as $category ) {
       	if ( $category->term_id =('1")= $cat_offset ) {
       		$offset = 1;
       	}
       }
       ?>
       ```
   
 * My troubles seem to be happening on this line as I’m not sure about the proper
   syntax:
 * `if ( $category->term_id =('1")= $cat_offset ) {`
 * Thanks again!
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650810)
 * “_I would like to avoid duplicating Category 1_“
 * Um, not sure I understand. Why are you trying to avoid duplicating it? And where
   is it *not* supposed to be duplicated?
 *  Thread Starter [Thad Allender](https://wordpress.org/support/users/endortrails/)
 * (@endortrails)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650811)
 * Perhaps I just confused you. I’m simply having troubling following your directions
   on where to insert the category number. My category number is 1. Being new to
   php, I’m causing fatal errors when/where I attempt to insert category 1 in the
   php.
 * >  Again, if necessary modify the associative array key (‘term_id’) on $category-
   > >term_id to the appropriate one.
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650812)
 * Ah! No, the category number (assuming this is your Main Feature category) would
   go in the first bit of code I have above (the code statement meant for the Main
   Feature post loop). This line specifically:
 * `if ( $category->term_id != '100' ) {`
 * Change 100 to 1. There should be no need to do anything to the code statement
   going before the other queries.
 *  Thread Starter [Thad Allender](https://wordpress.org/support/users/endortrails/)
 * (@endortrails)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650813)
 * Huh. Interesting. Well, then I guess it should be working because that is exactly
   the code that I have running live on [my site](http://thadallender.com). Doesn’t
   seem like it’s working.
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650814)
 * Where in the template does “Main Feature” fall? Also, what are you using to generate
   the various queries?
 *  Thread Starter [Thad Allender](https://wordpress.org/support/users/endortrails/)
 * (@endortrails)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650815)
 * This is the Feature Category post code:
 * >     ```
   >     <!--BEGIN FEATURE-->
   > 
   >         <h3>
   > 
   >         <?php wp_list_categories('include=1&title_li=&style=none'); ?></h3>
   > 
   >         <hr>
   > 
   >         <?php if (have_posts()) : ?>
   >         <?php while (have_posts()) : the_post(); ?>
   > 
   >         <?php
   >         $categories = wp_get_object_terms($post->ID, 'category');
   >         foreach( $categories as $category ) {
   >         if ( $category->term_id != '1' ) {
   >         $cat_offset = $category->term_id;
   >         }
   >         }
   >         ?>
   > 
   >     [Edited by moderator]
   >     ```
   > 
 * **I have five queries similar to this one on the page too:**
 * >     ```
   >     <h6><?php wp_list_categories('include=4&title_li=&style=none'); ?></h6>
   > 
   >     <hr>
   > 
   >     <?php
   >     $offset = 0;
   >     $categories = wp_get_object_terms($post->ID, 'category');
   >     foreach( $categories as $category ) {
   >     	if ( $category->term_id == $cat_offset ) {
   >     		$offset = 1;
   >     	}
   >     }
   >     ?>
   > 
   >     		  <?php query_posts('showposts=1&cat=4&offset=$offset'); ?>
   > 
   >         <?php while (have_posts()) : the_post(); ?>
   > 
   >     [Edited by moderator]
   >     ```
   > 
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650816)
 * Edited your last one as there was a lot of superfluous code not needed for dealing
   with the problem (also made it hard to read).
 * Anyway, here may be the problem:
 * `<?php query_posts('showposts=1&cat=4&offset=$offset'); ?>`
 * In PHP anything within single quotes is interpreted literally (so $offset is 
   passed as the text string ‘$offset’). Change it to double quotes so PHP will 
   read in and pass the variable you set:
 * `<?php query_posts("showposts=1&cat=4&offset=$offset"); ?>`
 *  Thread Starter [Thad Allender](https://wordpress.org/support/users/endortrails/)
 * (@endortrails)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650818)
 * Hmmm. That doesn’t do it either. To reiterate my dilemma to make sure we’re still
   on the same page:
 * I have five post queries that display the last post in each category. I have 
   one “Feature” that displays the latest post in all categories. So that I don’t
   have redundant posts on the page, the queries need to check to see which category
   needs to be offset.
 * So far, nothing seems to work.
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650820)
 * Difficult to troubleshoot this way, so if you could paste your template up here:
 * [http://wordpress.pastebin.ca/](http://wordpress.pastebin.ca/)
 * and reply with the url to it, I’ll take a look.
 *  Thread Starter [Thad Allender](https://wordpress.org/support/users/endortrails/)
 * (@endortrails)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650821)
 * Thanks Kaf. Here you go:
 * [http://wordpress.pastebin.ca/772131](http://wordpress.pastebin.ca/772131)
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [18 years, 6 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/#post-650822)
 * Ok, looking over the template I need to go back to your original topic post. 
   You stated:
 * >  I have a “Main Feature” category where I post my latest entry
 * I interpreted this to mean the post in this section of your front page was actually
   added to a post category called “Main Feature,” along with the category it normally
   fell under. However, it seems the Main Feature post is intended as the latest
   post on the blog as a whole.
 * I also got deeper in with your issue on duplicating category 1, which I assumed
   was the “Main Feature” category #.
 * Since the code I gave you assumes there is a Main Feature category, I’ll make
   some changes to it dealing with that. Let me look things over later today when
   I have time, then I’ll paste up a mod of your template.

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/query-post-within-category-and-offset/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/query-post-within-category-and-offset/page/2/?output_format=md)

The topic ‘Query Post Within Category and Offset’ is closed to new replies.

## Tags

 * [offset](https://wordpress.org/support/topic-tag/offset/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * 21 replies
 * 3 participants
 * Last reply from: [kasiblog](https://wordpress.org/support/users/kasiblog/)
 * Last activity: [17 years, 8 months ago](https://wordpress.org/support/topic/query-post-within-category-and-offset/page/2/#post-650945)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
