I want to echo the pages only with the custom taxonomy, but now i have drop down all pages what i have in custom post type.
<?php
$post_type_object = get_post_type_object('property');
if ($post) {
$parent_properties_dropdown_args = array(
'post_type' => 'property',
'property_status' => 'condominium-villas-project', // <<-- Here is the (property_status - is the custom taxonomy and condominium-villas-project is the custom taxonomy tag)
'selected' => $prop_data->post_parent,
'name' => 'parent_id',
'show_option_none' => __('Not selected'),
'echo' => 0,);
$parent_properties_dropdown = wp_dropdown_pages($parent_properties_dropdown_args);
if (! empty($parent_properties_dropdown)) {
?>
<div class="form-option parent-field-wrapper">
<label for=""></label>
<?php echo $parent_properties_dropdown; ?>
</div>
<?php
} }
But im anyway get all pages in custom post type 'property'. i need only 'property' with the taxonomy to show.
example: now -> - Property 1 - 'property_status' 'rent' - Property 2 - 'property_status' 'sale' - property 3 - 'property_status' 'condominium-villas-project'
i want to get only - property 3 - 'property_status' 'condominium-villas-project'
Using what I think is your exact code (I am still confused by your use of $prop_data
) you could use the following code to get what (I think) you are looking for. Add the code to your theme's function.php
file, a .php
file it requires, or one of the .php
files of a plugin you might implement:
<?php
class wpse_51782342 {
static function on_load() {
add_filter( 'get_pages', [ __CLASS__, '_get_pages' ], 10, 2 );
}
static function _get_pages( $pages, $args ) {
if ( isset( $args[ 'property_status' ] ) ) {
$pages = get_posts(array(
'post_type' => 'property',
'posts_per_page' => -1,
'property_status' => $args[ 'property_status' ],
'include' => wp_list_pluck( $pages, 'ID' ),
));
}
return $pages;
}
}
wpse_51782342::on_load();
The 'get_pages'
filter hook runs at the end of get_pages()
which is called by wp_dropdown_pages()
. In that hook the code looks for the argument you passed named 'property_status'
to decide if it should modify behavior. This is an important technique because it ensures that the same args will always return the same results and are not dependent on something like the current post ID or URL. Following this principle will usually reduce the number of bugs you have to fix in your project.
If the argument 'property_status'
is found the $args
array the code uses its value to call get_posts()
to return a list of posts that have been assigned the value of property_status
that you passed to wp_dropdown_pages()
.
Finally the code limits the get_posts()
query to the $post->ID
s from $pages
found by the query already run by wp_dropdown_pages()
. This should result in a dropdown showing just the pages you prefer.
And for reference, here is the code in single.php
to test out the above code, after I entered examples for property and property status, of course.
wp_dropdown_pages(array(
'post_type' => 'property',
'property_status' => 'condominium-villas-project',
'selected' => $post->ID,
'name' => 'ID',
'show_option_none' => __('Not selected'),
'echo' => 1,
));
Hope this helps?
You cannot filter wp_dropdown_pages()
with custom taxonomy. You can use normal WordPress query like below.
<?php
$the_query = new WP_Query( array(
'post_type' => 'property',
'tax_query' => array(
array (
'taxonomy' => 'property_status',
'field' => 'slug',
'terms' => 'condominium-villas-project',
)
),
) );
if ( $the_query->have_posts() ) :
?>
<div class="form-option parent-field-wrapper">
<label for=""></label>
<select name='parent_id' id='parent_id'>
<option value="">Not selected</option>
<?php
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<option value="<?php the_ID(); ?>"><?php the_title(); ?></option>
<?php endwhile; ?>
</select>
</div>
<?php
endif;
wp_reset_postdata();
?>