2012-01-10 94 views
0

我想抓住一個特定帖子類型的帖子列表和它被分配的類別ID。自定義查詢與自定義帖子類型和類別ID

所以從網上搜索我設法拿出

function fs_tpl_get_results($post_type, $mycat) { 
    global $wpdb; 


    $args = wp_parse_args($args,array(
    'post_type'  => '$post_type', 
    'term_id'   => 22, 
)); 
    extract($args); 


    $sql = <<<SQL 
SELECT DISTINCT 
    {$wpdb->terms}.*, 
    COUNT(*) AS post_count 
FROM 
    {$wpdb->terms} 
    INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id={$wpdb->term_taxonomy}.term_id 
    INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_taxonomy}.term_taxonomy_id={$wpdb->term_relationships}.term_taxonomy_id 
    INNER JOIN {$wpdb->posts} ON {$wpdb->term_relationships}.object_id={$wpdb->posts}.ID 
    INNER JOIN {$wpdb->term_relationships} related_relationship ON {$wpdb->posts}.ID=related_relationship.object_id 
    INNER JOIN {$wpdb->term_taxonomy} related_term_taxonomy ON related_relationship.term_taxonomy_id=related_term_taxonomy.term_taxonomy_id 
    INNER JOIN {$wpdb->terms} related_terms ON related_term_taxonomy.term_id=related_terms.term_id 
WHERE 1=1 
    AND (related_term_taxonomy.taxonomy<>{$wpdb->term_taxonomy}.taxonomy OR related_terms.term_id<>{$wpdb->terms}.term_id) 
    AND {$wpdb->posts}.post_type='%s' 
    AND related_terms.term_id=%d 
GROUP BY 
    {$wpdb->terms}.term_id 
SQL; 
    $sql = $wpdb->prepare($sql,$post_type,$term_id); 



    $results=$wpdb->get_results($sql); 

    return $results; 
} 

但它返回一個空的結果到目前爲止,沒有任何人有任何想法解決這個問題嗎?

回答

0

有建於WordPress的功能,可以這樣做:

<?php 
$args = array(
    'category_name' => 'your_csategoty_name', 
    'post_type' => 'your_custom_posttype' 
); 
// The Query 
$the_query = new WP_Query($args); 

// The Loop 
while ($the_query->have_posts()) : $the_query->the_post(); 
    //inside the loop 
    the_title(); 
    the_content(); 
endwhile; 

// Reset Post Data 
wp_reset_postdata(); 

?> 

http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

0

下面是一個代碼:

$args = array('post_type' => 'post','meta_query' => array(array('key' => 'cat','value' => 'yes','compare' => '%'))); 









$cat = new WP_Query($args); 







// The Loop 

while ($cat->have_posts()) : $cat->the_post(); 





    $P_ID = get_the_ID(); 



endwhile; 



// Reset Post Data 

wp_reset_postdata(); 

$post = wp_get_single_post($P_ID); 

如果它不能正常工作更改頁面ID

相關問題