1

I would love to get the list of posts by their custom taxonomy (=store) name.

Here is what I have so far, but it is not working.

Please help with the code.

$mystorename is a variable holding the name of the store i want to query by.

Any help will be appreciated.

Thank you

$args = array(
    'tax_query' => array(
    array(
        'taxonomy' => 'store',
        'field' => 'name',
        'terms' => $mystorename
    )
)
);

$postslist = get_posts( $args );if(count($postslist) > 0){ ?>
1

I'm not sure if the get_posts function supports the tax_query. You might want to try creating a new WP_Query object instead.

$args = array(
'tax_query' => array(
    array(
        'taxonomy' => 'store',
        'field' => 'name',
        'terms' => $mystorename
        )
    )
);

$query = new WP_Query($args);
if ( $query -> have_posts() ) : while ( $query -> have_posts() ) : $query -> the_post(); ?>
    <!-- post -->
<?php endwhile; ?>
    <!-- post navigation -->
<?php else: ?>
    <!-- no posts found -->
<?php endif; ?>
  • the query you posted above kept giving me an error ("expected )") on the line of the last "array(".I had no idea what was wrong, so I abandoned it and went for the code below, which works perfectly. Thanks – Naijadeals Sep 8 '12 at 1:20
0
 <?php
 $args=array(
  'store' => $mystoreslug,
'post_type' => 'post',
'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() ) {?>

 <div class="itembox">
 <h1>Store Coupons</h1>

<div class="itemboxinner">

 <?php 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(); ?>"><h3><?php the_title(); ?></h3></a></p>
  <?php
 endwhile;
}
wp_reset_query();  
?>'
0

How about...

$args = array(
    'store' => $mystorename,
);
$postslist = query_posts( $args );
wp_reset_query();
  • 2
    Explain why that could solve the problem. – fuxia Dec 7 '12 at 15:44
  • Query posts should be used last resort, use get_posts instead. Should $mystorename be a slug, id, display name? explain – AlxVallejo Dec 17 '12 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.