• Hi. I’m using WordPress 2.9.2. I want to query both posts and pages that have the custom field “Banner home”. So, I’m doing a query_posts call.
    But instead I set post_type=any, the query doesn’t works. It only works If I call to an specific category or page.

    My code looks like this:

    <?php $args=array('post_type' => 'any', 'post_status' => 'publish', 'meta_key' => 'Banner home', 'posts_per_page' => 3);
    	$my_query_bannerhome = new WP_Query($args); ?>
        <?php if ($my_query_bannerhome->have_posts()) : while ($my_query_bannerhome->have_posts()) : $my_query_bannerhome->the_post(); ?>
        <div class="post" id="post-<?php the_ID(); ?>">
    	// My stuff
        </div>
        <?php endwhile; endif; ?>

    What I’m doing wrong?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Works fine for me:

    <?php
    $args=array(
      'meta_key'=>'cf1',
      'post_type' => 'any',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      '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(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    Thread Starter chavo

    (@chavo)

    Hi Michael. Mmmmm…I’s weird. It’s doesn’t work for me. I just change your code to this:

    <?php
    $args=array(
      'meta_key'=>'Banner home',
      'post_type' => 'any',
      'post_status' => 'publish',
      'posts_per_page' => 3,
      '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(); ?>
        <div class="post" id="post-<?php the_ID(); ?>">
    <?php if ( function_exists( 'get_the_image' ) ) { get_the_image(
    		array(
    		'custom_key' => array( 'Banner home' ),
    		'default_size' => '',
    		'attachment' => false,
    		'link_to_post' => true,
    		'image_scan' => false,
    		'width' => '980',
    		'height' => '350',
    		)); } ?>
    </div>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    I’m doing other query calls in the same page, but if I put the code alone, nothing happens.

    My code only works If I use this:

    `<?php $args=array(‘post_type’ => ‘any’, ‘post_status’ => ‘publish’, ‘meta_key’ => ‘Banner home’, ‘posts_per_page’ => 3, ‘category_name’=>’noticias’);
    $my_query_bannerhome = new WP_Query($args); ?>
    <?php if ($my_query_bannerhome->have_posts()) : while ($my_query_bannerhome->have_posts()) : $my_query_bannerhome->the_post(); ?>
    <div class=”post” id=”post-<?php the_ID(); ?>”>
    <?php if ( function_exists( ‘get_the_image’ ) ) { get_the_image(
    array(
    ‘custom_key’ => array( ‘Banner home’ ),
    ‘default_size’ => ”,
    ‘attachment’ => false,
    ‘link_to_post’ => true,
    ‘image_scan’ => false,
    ‘width’ => ‘980’,
    ‘height’ => ‘350’,
    )); } ?>
    </div>
    <?php endwhile; endif; ?>`

    If I remove ‘category’=>’noticias’, the query doesn’t display any post instead of ‘post_tye’=>’any’

    Thread Starter chavo

    (@chavo)

    I just added a “Banner home” custom field to a page and remove ‘category’=>’noticias’ and the query display it, but doesn’t display posts in categories. So, my code looks like this now:

    <?php $args=array('post_type' => 'any', 'post_status' => 'publish', 'meta_key' => 'Banner home', 'posts_per_page' => 3);
    	$my_query_bannerhome = new WP_Query($args); ?>
        <?php if ($my_query_bannerhome->have_posts()) : while ($my_query_bannerhome->have_posts()) : $my_query_bannerhome->the_post(); ?>
        <div class="post" id="post-<?php the_ID(); ?>">
    	<?php if ( function_exists( 'get_the_image' ) ) { get_the_image(
    		array(
    		'custom_key' => array( 'Banner home' ),
    		'default_size' => '',
    		'attachment' => false,
    		'link_to_post' => true,
    		'image_scan' => false,
    		'width' => '980',
    		'height' => '350',
    		)); } ?>
        </div>
        <?php endwhile; endif; ?>

    It still doesn’t shows all the post with this custom field.

    ps: sorry for my bad english.

    Wonder if there’s a plugin causing the problem. Might deactivate plugins and if necessary switch to the WordPress Default theme then put your code in the wp-content/themes/default/index.php file.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘query_post by meta_key only without specific category setting not works’ is closed to new replies.