2012-01-10 1 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

이는이 작업을 수행 할 수있는 워드 프레스 기능이 내장되어 있습니다 :

<?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

코드입니다 :이 페이지의 ID를 변경 작동하지 않는 경우

$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); 

관련 문제