Chris
Chris

Reputation: 41

Filter Wordpress Data in Array

I have following Wordpress Code, which results in a list of properties.

    $args = array( 'posts_per_page' => 5, 'post_type'=> 'property');

    $myposts = get_posts( $args );

    foreach ( $myposts as $property_data ): 

        $meta = get_post_meta( $property_data->ID );

        $vdo_url = wp_get_attachment_url( $meta[fave_video_image][0] );
        $images =  wp_get_attachment_url( $meta[fave_property_images][0]);
        $city = wp_get_post_terms($property_data->ID, 'property_city');
        $type = wp_get_post_terms($property_data->ID, 'property_type');
        $status1 = wp_get_post_terms($property_data->ID, 'property_status');

        ?>
                <tr>
                    <td class="date">
                      <h5><input type="checkbox" class="propcheckbox" id="property" name="filedata[]" value="<?php echo $property_data->ID; ?>"> <?php echo $property_data->post_title; ?></h5>
                    </td>
                    <td class="hidden-xs hidden-sm">
                        <?php echo get_the_post_thumbnail( $property_data->ID ); ?>
                    </td>
                    <td>
                        <h5><?php echo $city[0]->name; ?></h5>
                    </td>
                    <td class="text-center">
                        <h5><?php echo $type[0]->name; ?></h5>
                    </td>
                    <td class="text-center">
                        <h5><?php echo $status1[0]->name ?></h5>
                    </td>
                    <td>
                        <h5><?php echo $property_data->post_date;  ?></h5>
                    </td>
                </tr>

    <?php endforeach; 

I want now to show only the properties where the property_status is not "verkauft". The property_status is in the wp_terms table.

What do I need to add/change?

Upvotes: 0

Views: 236

Answers (2)

ibenic
ibenic

Reputation: 319

You can do it on the query level:

$args = array( 
 'posts_per_page' => 5, 
 'post_type'=> 'property',
 'tax_query' => array(
    array(
        'taxonomy' => 'property_status',
        'field'    => 'slug',
        'terms'    => array( 'verkauft' ),
        'operator' => 'NOT IN'
    ),
  ),
);

$myposts = get_posts( $args );
...

This should get you all the properties that do not have that status. You can test it and see if that is correct.

Read more on the taxonomy queries here: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Upvotes: 1

Steve
Steve

Reputation: 20469

The simplest option would be to filter in php and skip output if the condition is met:

foreach ( $myposts as $property_data ): 
    //get the status 1st
    $status1 = wp_get_post_terms($property_data->ID, 'property_status');
    //if the status matches, skip the rest of this loop iteration
    if($status1=='verkauft') continue;

    $meta = get_post_meta( $property_data->ID );

    //the rest of your code

Upvotes: 0

Related Questions