wp_dropdown_pages (Show pages only with custom taxonomy)?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



wp_dropdown_pages (Show pages only with custom taxonomy)?



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'





What is $prop_data? Should that not be $post?
– MikeSchinkel
Aug 10 at 9:55


$prop_data


$post




2 Answers
2



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:


$prop_data


function.php


.php


.php


<?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.


'get_pages'


get_pages()


wp_dropdown_pages()


'property_status'



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().


'property_status'


$args


get_posts()


property_status


wp_dropdown_pages()



Finally the code limits the get_posts() query to the $post->IDs from $pages found by the query already run by wp_dropdown_pages(). This should result in a dropdown showing just the pages you prefer.


get_posts()


$post->ID


$pages


wp_dropdown_pages()



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.


single.php


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?





Thanks for a help
– Ruslan
Aug 10 at 13:24





@Ruslan great!. If this is a good answer, can you give it an upvote too? Thanks/
– MikeSchinkel
Aug 10 at 20:30





:) When i finish i will do it, sure ) I'm try to add you code to works )))
– Ruslan
Aug 11 at 7:22





Totaly done, works perfectly. I'm just add your 1st park of the code before the im use wp_dropdown_pages and then it works. Now in drop down i saw only pages with the 'property_status' => 'condominium-villas-project'. Thanks a lot for a help!
– Ruslan
Aug 11 at 7:38



You cannot filter wp_dropdown_pages() with custom taxonomy. You can use normal WordPress query like below.


wp_dropdown_pages()


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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard