简体   繁体   English

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

[英]WordPress Get All Posts With Specific Taxonomy

I'm creating a custom PHP script that adds posts to WordPress and allows you to view/modify posts. 我正在创建一个自定义PHP脚本,该脚本将帖子添加到WordPress,并允许您查看/修改帖子。

The WordPress Theme I'm using has specific post_type , and specific variables to that type. 我正在使用的WordPress主题具有特定的post_type和该类型的特定变量。

I can add posts without any issues, but I'm having a difficult time trying to query all posts with the specific tax_input value. 我可以添加帖子,没有任何问题,但是我很难查询具有特定tax_input值的所有帖子。

Here is the code that adds posts: 这是添加帖子的代码:

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

What do I need to do to get posts with the same tax_input value? 我需要怎么做才能获得具有相同tax_input值的帖子?

The below code gets all the properties but with all property-status values, I want to show only a certain properties with certain property-status values: 下面的代码获得所有的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();

Thanks in advance! 提前致谢!

I have resolved my issue once I found the Wordpress documentation for taxonomies. 找到分类法的Wordpress文档后,我已经解决了问题。

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

The working code: 工作代码:

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

Enjoy! 请享用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM