• Resolved wheelz13

    (@wheelz13)


    I’m creating a custom PHP script that adds posts to WordPress and allows you to view/modify posts.

    The WordPress Theme I’m using has specific post_type, and specific variables to that 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.

    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 <code>term_id</code> 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?

    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:

    $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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You want to add a tax_query argument to get_posts(). The link is for the WP_Query class, any arguments valid for WP_Query are valid for get_posts().

    Thread Starter wheelz13

    (@wheelz13)

    Thank you, that was what I was looking for!

    My working code:

    $postArgs = array(
    				'posts_per_page' => 25,
    				'post_type'     => 'property',
    				'tax_query' => array(
    					array(
    						'taxonomy' => 'property-status',
    						'field' => 'term_id',
    						'terms' => '48'
    					)
    				)
    			);
    $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();
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WordPress Get All Posts With Specific Taxonomy’ is closed to new replies.