Custom Post Type Query for Sidebar Doesn’t Work on Front Page

Interesting issue here.

I have this:

Read More
$args=array(
'meta_key'=>'_simple_fields_fieldGroupID_1_fieldID_8_numInSet_0',
  'post_type' => 'stores',
  'post_status' => 'publish',
  'posts_per_page' => 10,
  'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
<ul><li>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php $selected_value = get_post_meta(get_the_id(), "_simple_fields_fieldGroupID_1_fieldID_9_numInSet_0", true);
  echo "$selected_value"; ?></a>&nbsp;at&nbsp;<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</li></ul>
<?php
  endwhile;

}
wp_reset_query();  // Restore global post data stomped by the_post().

Which intends to display a list of a specific post type (stores) in the sidebar if a specific custom field is not empty. On single posts that works great – on the main page (home.php) it doesn’t – the sidebar widget is empty.

I’m stumped. Any ideas?

John

Related posts

Leave a Reply

1 comment

  1. This is your code converted so it does not use global variables, and thus can’t stomp on anything. If this does not work, check your plugins: maybe one of them uses a hook in WP_Query to change the query on the home page? get_posts() gets around that by setting suppress_filters to true, but I don’t know whether that disables all hooks.

    $args = array(
        'meta_key'=>'_simple_fields_fieldGroupID_1_fieldID_8_numInSet_0',
        'post_type' => 'stores',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'caller_get_posts'=> 1
    );
    $sidebar_posts = get_posts( $args );
    if ( $sidebar_posts ) {
        // Do you want this for each post, or just once for the list?
        echo '<ul>';
        foreach ( $sidebar_posts as $s_post ) {
            echo '<li>';
            $post_link = '<a href="' . get_permalink( $s_post->ID ) . '" rel="bookmark" title="Permanent link to ' . esc_attr( get_the_title( $s_post->ID ) . '">';
            $selected_value = get_post_meta( $s_post->ID, '_simple_fields_fieldGroupID_1_fieldID_9_numInSet_0', true );
            echo $post_link . $selected_value . '</a>&nbsp;at&nbsp;';
            echo $post_link . get_the_title( $s_post->ID ) . '</a>';
            echo '</li>';
        }
        echo '</ul>';
    }