• I want to know how to get posts that is under a specific category with a custom filed value.

    for example: a post hello world is under Cat1 and has a custom value like country = USA

    Please anyone let me know how to write query.

Viewing 3 replies - 1 through 3 (of 3 total)
  • I think this works:

    <?php
    $args=array(
      'showposts' => 3,
      'category__in' => array(3,5),
      'caller_get_posts'=>1
      'meta_key' => 'country',
      'meta_value' => 'USA'
    );
    $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 vrktech

    (@vrktech)

    Hi MichaelH, Thanks for your input, I tried that, it works but my requirement is a bit different

    I am looking to add this code to index.php, category.php

    country is added as a custom field.

    The site works as usual wp install, and when the user selects a particular country then it keeps the country in a session variable. Again the site works normally but only the posts for that country are displayed.

    I added this

    $args=array(
      'showposts' => 3,
      'category__in' => array(3,5),
      'caller_get_posts'=>1
      'meta_key' => 'country',
      'meta_value' => 'MY SESS Variable here'
    );

    It is not working when I select a category (I replaced ‘category__in’=>$catid with is_category() )

    //if category archive assign queried category
    if ( is_category() ) {
    $catid = get_query_var('cat');
    }
    $args=array(
      'showposts' => 3,
      'category__in' => array($catid),
      'caller_get_posts'=>1
      'meta_key' => 'country',
      'meta_value' => 'MY SESS Variable here'
    );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get Posts with specific custom value and specific category’ is closed to new replies.