【问题标题】:Get posts from 2 categories and different taxonomies.从 2 个类别和不同的分类法中获取帖子。
【发布时间】:2013-06-21 23:51:17
【问题描述】:

我有一个自定义帖子(称为“ait-dir-items”)和 2 个类别。 在分类存档页面上,我需要获取属于 2 个分类的帖子。

从 2 个类别中获取帖子

编码。 category1 = "ait-dir-item-category" 上的 "food" (food id 为 6) 和 category2 = “ait-dir-item-category”上的“cate”(食物 ID 为 39) 和 post_type="ait-dir-items"

我一直在尝试 3 种方法来解决这个问题。 他们都没有很好地工作。请告诉我如何解决它。

第一种方式

query_posts("cat=6, 39&showposts=5&post_type=ait-dir-item");
//query_posts( array( post_type=>'ait-dir-item', 'category__and' => array(6,39),         'posts_per_page' => 12, 'orderby' => 'title', 'order' => 'DESC' ) );

while(have_posts()) : the_post();

echo $title = get_the_title();
echo $content = get_the_content();
endwhile;

当我输入 "cat=6, 39" 或 "'category__and' => array(6,39)" 时,没有找到结果。

第二种方式

global $post;

$tmp_post = $post;

$args = array(
'posts_per_page' => 5,
'post_type' => 'ait-dir-item',
'category__and' => array(6, 39) // where 1, 2 is cat id
/*'tax_query' => array(
array(
'taxonomy' => 'ait-dir-item-special',
'field' => 'id',
'terms' => 39 // taxonomy id
)
)*/
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) :

$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail_size' );
$url = $thumb['0'];
?>

<li class="display_special_items"><img src="<? echo $url; ?>"/>"><?php the_title(); ?>
<?php endforeach; ?>
<?php $post = $tmp_post; ?>
<li class="clear">

第三种方式:关系和与


$custom_terms = get_terms('ait-dir-item-special');
$other_custom_terms = get_terms('ait-dir-item-category');

foreach ($custom_terms as $custom_term) {
foreach ($other_custom_terms as $other_custom_term) {
wp_reset_query();
$args = array('post_type' => 'ait-dir-item',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'ait-dir-item-category',
'field' => 'id',
'terms' => 6
),
array(
'taxonomy' => 'ait-dir-item-special',
'field' => 'id',
'terms' => 39
),
),
);

$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h1 style="margin-top:10px;">'.$custom_term->name.'</h1>';

while($loop->have_posts()) : $loop->the_post();
echo '<h2>'.get_the_title().'</h2>';
endwhile;
}
}
}

` 我想我对这些代码没有什么问题。它显示但重复和所有项目帖子。我该如何解决?

谢谢,

【问题讨论】:

    标签: wordpress categories posts


    【解决方案1】:

    我有类似的情况,找不到答案:

    • 正在检索属于 custom_taxonomy1 并且也属于 custom_taxonomy2 的相同 custom_type 的所有帖子。

    示例:我需要属于某个政党 (custom_taxonomy2) 的所有 SectorA (custom_taxonomy1) 提案:

    我最终使用了以下适用于我的案例的查询,您可能会得到一个想法并将其改编为您的:

    SELECT 
    wposts.ID, 
    wposts.post_title, 
    wposts.post_type, 
    wposts.post_status,
    
    termRels.object_id termObjectId, 
    termRels.term_taxonomy_id termTaxId, 
    terms.name partido
    
    FROM $wpdb->posts wposts
    
    INNER JOIN $wpdb->terms terms  
    ON terms.term_id = $p 
    
    INNER JOIN $wpdb->terms termsSector  
    ON termsSector.term_id = $sector   
    
    INNER JOIN $wpdb->term_taxonomy termTax 
    ON termTax.term_id = terms.term_id 
    
    INNER JOIN $wpdb->term_taxonomy termTax2  
    ON termTax2.term_id = termsSector.term_id  
    
    INNER JOIN $wpdb->term_relationships termRels  
    ON termRels.term_taxonomy_id = termTax.term_taxonomy_id  
    
    INNER JOIN $wpdb->term_relationships termRelsSector   
    ON termRelsSector.term_taxonomy_id = termTax2.term_taxonomy_id  
    
    WHERE wposts.post_type = 'likan_planes'
    
    AND wposts.post_title != 'Auto Draft' 
    AND wposts.post_status = 'publish' 
    
    AND wposts.ID = termRels.object_id 
    AND termRels.object_id = termRelsSector.object_id 
    
    ORDER BY wposts.post_title ASC 
    

    【讨论】:

      【解决方案2】:

      您的语法不正确。请遵循 Wordpress 的“query_posts”参考指南!

      请在此处阅读http://codex.wordpress.org/Function_Reference/query_posts

      你需要使用这个:

      query_posts( array( 'category__and' => array(1,3), 'posts_per_page' => 2, 'orderby' => 'title', 'order' => 'DESC' ) );
      

      您可以根据需要使用其他类别的过滤器!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-16
        • 2014-11-18
        • 2011-08-29
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多