Title: Loop Queries Effecting Sidebar Queries
Last modified: August 18, 2016

---

# Loop Queries Effecting Sidebar Queries

 *  [tadramgo](https://wordpress.org/support/users/tadramgo/)
 * (@tadramgo)
 * [19 years, 7 months ago](https://wordpress.org/support/topic/loop-queries-effecting-sidebar-queries/)
 * Hi, I’m pretty new to wordpress, so I’m not sure about all of it’s nuances.
 * I’m running a basic (no non-default plugins) setup as a CMS, writers website.
   I have two custom fields for each post. “Post Author” and “Featured Details”.
   There are a few catagories, Poetry, Prose, Editorial etc.
 * Basically my content and comments are displaying fine with The Loop. However 
   I am using custom Archive Pages that use the
 * foreach($posts as $post) :
 * loop formatting, much like the main page, displaying page titles / author details
   within it as appropriate. However my sidebar uses
 * if (get_post_meta($post->ID, “Poem Author”, 1)) { etc }
 * To create a menu with “Authors Other Works” (another foreach) loop depending 
   on the meta data. This is fine on pages belonging to that author, however on 
   these custom archive pages it means that you have a list of “Other Works” for
   whoever was last on the Archive list. Whoever was last accessed by the loop. 
   I don’t want this to happen. Is there anyway to separate loops from each other?
   Or do I need to find a different method of achieving this?
 * I hope I’m making sense. Thanks for your help.
 * Tom ~

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [19 years, 7 months ago](https://wordpress.org/support/topic/loop-queries-effecting-sidebar-queries/#post-468515)
 * What would you want on the custom archives page’s (Page?) sidebar? Can’t you 
   make
    a) a conditional in it to show something else or b) another sidebar for
   the archives page?
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [19 years, 7 months ago](https://wordpress.org/support/topic/loop-queries-effecting-sidebar-queries/#post-468536)
 * The issue is the use of `$posts` in your custom code, which is whacking the original
   posts object (i.e. `$posts`) WordPress is passing along.
 * `foreach($posts as $post) :`
 * Something is missing here, that is where `$posts` is assigned the actual post(
   s) data. Is there something like:
 * `$posts = get_posts('`_blah blah blah_`'):`
 * just above it? Whatever the case, rename `$posts` throughout your code to something
   else, like `$arc_posts`.
 *  Thread Starter [tadramgo](https://wordpress.org/support/users/tadramgo/)
 * (@tadramgo)
 * [19 years, 7 months ago](https://wordpress.org/support/topic/loop-queries-effecting-sidebar-queries/#post-468580)
 * I’ve been trying to solve the problem by defining values in the index.php and
   then relying on them to indicate to the sidebar that I want to show menu items…
 * eg in index.php
 * if (condition) {
    $useMenu = 1; }
 * in sidebar.php
 * if ($useMenu=1;) {
    show sidebar }
 * however for some reasons these variables aren’t available from one page to another
   using the wordpress “get_sidebar()” function. After testing for a while this 
   is what I’ve concluded… is there anyway to make variables global, or pass variables
   to the function?
 * I know this is a bit of a hacky solution, but if it works…
 * cheers,
    tom
 *  [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * (@kafkaesqui)
 * [19 years, 7 months ago](https://wordpress.org/support/topic/loop-queries-effecting-sidebar-queries/#post-468597)
 * `if (condition) {
    global $usemenu; $useMenu = 1; }
 * And in sidebar.php:
 * `global $usemenu;
    if ($useMenu == 1) { show sidebar }
 *  Thread Starter [tadramgo](https://wordpress.org/support/users/tadramgo/)
 * (@tadramgo)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/loop-queries-effecting-sidebar-queries/#post-468732)
 * Globals! I completely forgot about them… isn’t global variables usually turned
   off in php? Or does defining them as you did override the basic php settings?
 * I got around all this by using include() rather than wp_sidebar etc…

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Loop Queries Effecting Sidebar Queries’ is closed to new replies.

## Tags

 * [index](https://wordpress.org/support/topic-tag/index/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [queries](https://wordpress.org/support/topic-tag/queries/)

 * In: [Installing WordPress](https://wordpress.org/support/forum/installation/)
 * 5 replies
 * 3 participants
 * Last reply from: [tadramgo](https://wordpress.org/support/users/tadramgo/)
 * Last activity: [19 years, 5 months ago](https://wordpress.org/support/topic/loop-queries-effecting-sidebar-queries/#post-468732)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
