具有自定义帖子类型和类别ID的自定义查询

时间:2012-01-10 01:31:38

标签: php mysql wordpress

我正在尝试按特定的帖子类型和分配的类别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;
}

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; }

但它返回一个空的结果,有没有人有任何想法来解决这个问题?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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