WordPress获取具有特定分类的所有帖子

时间:2014-05-09 22:52:02

标签: php mysql wordpress

我正在创建一个自定义PHP脚本,可以将帖子添加到WordPress,并允许您查看/修改帖子。

我使用的WordPress主题具有特定的post_type,以及该类型的特定变量。

我可以毫无问题地添加帖子,但是我很难尝试查询具有特定tax_input值的所有帖子。

以下是添加帖子的代码:

include('../wp-config.php'); //Get WordPress Config
$new_listing = array(
    'post_title'    => $listing_title,
    'post_name'     => str_replace('-', ' ', $listing_title),
    'post_content'  => $listing_description,
    'tax_input'     => array('property-status' => $listing_phase),
    //$listing_phase is the `term_id` number from what I see in the database
    'post_status'   => 'publish',
    'post_type'     => 'property',
    'post_author'   => 1
);
$listing_id = wp_insert_post($new_listing); //Insert the post into the database
add_post_meta($listing_id, 'REAL_HOMES_banner_sub_title', $listing_subtitle);
add_post_meta($listing_id, 'REAL_HOMES_property_address', $listing_address);
add_post_meta($listing_id, 'REAL_HOMES_property_location', $listing_address_lat.','.$listing_address_lng);
add_post_meta($listing_id, 'REAL_HOMES_property_size', $listing_sqare_foot);
add_post_meta($listing_id, 'REAL_HOMES_property_size_postfix', 'Sq Ft');
add_post_meta($listing_id, 'REAL_HOMES_property_bedrooms', $listing_bedrooms);
add_post_meta($listing_id, 'REAL_HOMES_property_bathrooms', $listing_bathrooms);
add_post_meta($listing_id, 'REAL_HOMES_property_garage', $listing_garage);

要获取具有相同tax_input值的帖子,我需要做些什么?

以下代码获取了所有properties,但所有property-status值,我只想显示具有某些properties值的某个property-status

$postArgs = array('posts_per_page' => 25, 'post_type' => 'property');
$getListings = get_posts($postArgs);
foreach($getListings as $post) : setup_postdata($post);
?>
    <a href="<?=the_permalink()?>" class="deploy-toggle-1"><?=the_title()?></a>
    <div class="content"><p><?=the_content()?></p></div>
<?
endforeach; 
wp_reset_postdata();

提前致谢!

1 个答案:

答案 0 :(得分:-1)

一旦我找到了分类法的Wordpress文档,我就解决了我的问题。

http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

工作代码:

$postArgs = array(
            'posts_per_page' => 25,
            'post_type'     => 'property',
            'tax_query' => array(
                array(
                    'taxonomy' => 'property-status', //Tax_Input
                    'field' => 'term_id', //Or Slug
                    'terms' => '48' //Term ID or Slug Name
                )
            )
        );
$getListings = get_posts($postArgs);
foreach($getListings as $post) : setup_postdata($post);
?>
     <a>" class="deploy-toggle-1"><?=the_title()?></a>
     <div class="content"><p><?=the_content()?></p></div>
<?
endforeach;
wp_reset_postdata();

享受!