簡體   English   中英

WordPress獲取具有特定分類法的所有帖子

[英]WordPress Get All Posts With Specific Taxonomy

我正在創建一個自定義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();

提前致謝!

找到分類法的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();

請享用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM