当前位置: 动力学知识库 > 问答 > 编程问答 >

php - WP_Query isn't working when 'tag' arg is passed

问题描述:

My first question :) I want to show posts from custom post type with specific tag. I am using 'tag' => 'slug' but it doesn't work. I can't use categories, it must be filtered by tags. This is my code:

 $args = array(

'numberposts' => 2,

'post_type' => 'poslovi-oglasi',

'tag' => 'javascript',

'orderby' => 'rand',

'posts_per_page' => 2,

'meta_query' => array(

'relation' => 'OR',

array(

'key' => 'tip_oglasa',

'value' => 'premium',

),

array(

'key' => 'tip_oglasa',

'value' => 'standard',

)

)

);

$the_query = new WP_Query( $args );

echo '<aside id="single-jobs" style="line-height: 100%;">';

if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post();

echo '

<div style="float: left; width: 50px;">

<a href="' . get_the_permalink() . '?psl=wgt' . get_the_ID() . '">

<img src="'; $image = get_field('logo_single'); echo $image['url']; echo '" height="50" width="50">

</a>

</div>

<div style="float: right; text-align: left; margin-bottom: 25px; width: 160px;">

<a href="' . get_the_permalink() . '?psl=wgt' . get_the_ID() . '">

<h2><span>' . get_the_title() . '</span></h2>

</a>

<div class="single-jobs-location" style="color: #bbb;">' . get_field('ime_firme') . '</div>

<div class="single-jobs-location">

<a href="http://startit.rs/poslovi/"' . get_field('lokacija_pozicije') . '" style="color: #888;">' . get_field('lokacija_pozicije') . '</a>

</div>

</div>

<div class="clear"></div>';

endwhile; endif;

When I delete tag 'tag' => 'javascript', posts are showing, but all posts form that post type, not posts with specific tag.

Please help and thank you

网友答案:

I found solution! Somebody registered tag taxonomy for that post type with this slug 'oznaka'. So I just changed 'tag' => 'slug' to 'oznaka' => 'slug' and it works.

That site I needed this code for is one very complex custom WordPress website that is built by few seniors who worked on it before me. So I missed that little thing, it's probably tribute to inexperience.

Thanks to doublesharp I knew that I am on good path so I continued to search for an answer

分享给朋友:
您可能感兴趣的文章:
随机阅读: