1

Interesting issue here.

I have this:

$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

2
  • Is the sidebar completely empty, so no $before_widget or $after_widget content, or is it only the list that is missing? Have you considered using get_posts() instead of WP_Query() and wp_reset_query(), to prevent weird issues from overriding the main query?
    – Jan Fabry
    Commented Jan 3, 2011 at 11:57
  • The rest of the sidebar shows - but the specific list is empty on the home page. I took the code snipped from wordpress.org - I haven't tried the other options as, to be honest, my coding skills suck as much as the weather in Chicago.
    – John
    Commented Jan 3, 2011 at 13:05

1 Answer 1

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>';
}
6
  • That is awesome, thanks so much. I am getting this error, though: syntax error, unexpected ';' on line 15
    – John
    Commented Jan 3, 2011 at 21:36
  • @John: I forgot a closing paren, it should be esc_attr( get_the_title( $s_post->ID ) ). If you get another syntax error, it helps if you can tell me what's on that line, because we don't start at the same number.
    – Jan Fabry
    Commented Jan 3, 2011 at 22:30
  • Same crazy problem - error is fixed but the results still do not show on the main page. The widget is empty, but for the widget title.
    – John
    Commented Jan 3, 2011 at 22:51
  • OK quick update. I've been putzing around a bit and this is what I have: I changed $sidebar_posts = get_posts( $args ); into $sidebar_posts = get_posts( array( 'suppress_filters' => true ) ); - and this at least gets a partial output. All results are there, but the content of a second custom field (_simple_fields_fieldGroupID_1_fieldID_9_numInSet_0) isn't showing in the results. It's probably just the way I butchered the query - could you tell me how to construct the existing query with suppress_filters?
    – John
    Commented Jan 3, 2011 at 23:27
  • @John: You can add the 'suppress_filters' => true line to the $args array, and it should work. Be sure to place a comma after each line in the array, otherwise you get a syntax error.
    – Jan Fabry
    Commented Jan 4, 2011 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.