Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query Set Post_Status by Dropdown Selection WordPress

  • SOLVED

Hello all

I have a page template which returns a cpt query, based on the current user. As I have it right now I have the template duplicated several times, one for each type of post_status, which is pretty inefficient. I am looking for a piece of code that will allow me to set the post_status based on the item selected by the user in a dropdown menu.

Please paste code on an external paste bin for ease of use.

Note: I do not want to use query strings.


Here is the current template, you'll notice the dropdown right now has links to other pages :[[LINK href="http://pastie.org/5506842"]] http://pastie.org/5506842[[/LINK]]

Thanks for your time

Answers (2)

2012-12-10

Arnav Joy answers:

try this

in this code you have to change pageID to whatever page you want to redirect when dropdown gets changed , you also have to change the type1, type2 etc.. to your valid case
then change $post_status to whatever status they will take

<?php
/*
Template Name: Guides UI - Event List
*/


remove_action( 'genesis_post_content', 'genesis_do_post_content' );
add_action( 'genesis_post_content', 'child_do_content' );
function child_do_content() {

$pageID = 1111; // give here page id which you want to load when drop down change
?>
<div class="eventlist clearfix">
<div class="btn-toolbar">
Upcoming Reservations
</div>

<div class="btn-group">
<button class="btn btn-inverse">Sort Reservations</button>
<button class="btn btn-inverse dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="<?php echo get_permalink($pageID);?>">Reserved Trips</a></li>
<li><a href="<?php echo get_permalink($pageID);?>?trip_type=type1">Time Blocks</a></li>
<li><a href="<?php echo get_permalink($pageID);?>?trip_type=type2">Approved Trips</a></li>
<li><a href="<?php echo get_permalink($pageID);?>?trip_type=type3">Pending Trips</a></li>
</ul>
</div>


<div class="eventlist-items">
<ul>
<?php
global $post;
$trip_type = $_GET['trip_type'];
if( $trip_type == 'type1' )
$post_status = 'publish';
else if( $trip_type == 'type2' )
$post_status = 'pending';
else if( $trip_type == 'type3' )
$post_status = 'draft';
else
$post_status = 'trash'; // default case

$current_user = wp_get_current_user();
$all_events = new WP_Query(
array(
'post_type'=>'events',
'post_status'=>$post_status ,
'author'=>$current_user->ID,
));
while($all_events->have_posts()) : $all_events->the_post();
setup_postdata($post);

***Query output

endwhile; wp_reset_query(); ?>
</ul>
</div>
</div>

<?php
}
genesis();


Kyle comments:

Thanks for the reply, this looks good. I will test this out and come back


Kyle comments:

Works Great! Thanks for your help

2012-12-10

Dbranes answers:

Hi, so if I understand you right, then you want to have several different pages, using the same page template.

Then you want to have different the post_status in the $all_events query, depending on the page.

So you could fx. find the post_name of the current page and use it to select corresponding post_type like this:


global $post;
$slug = $post->post_name;
switch ( strtolower($slug) ) {
case 'slug_1':
$my_post_type = 'type_1';
break;
case 'slug_2':
$my_post_type = 'type_2';
break;
default:
$my_post_type = 'type_1';
break;
}


and then you could use this to get the post_type

$all_events = new WP_Query(
array(
'post_type'=>'events',
'post_status'=>$my_post_type,
'author'=>$current_user->ID,
));



Kyle comments:

Thank you for the reply,

I am looking for something a little different, I would ideally like to use only 1 page and one 1 page template, and simply re-sort and reload the page when the dropdown is selected and add the selected post_status to the query

I think your code is on the right track, but it needs to use the dropdown href="" or id="" for the value of the $my_post_type. Is that possible?


Dbranes comments:

ok ;-)

<blockquote>Note: I do not want to use query strings.</blockquote>

so you mean you don't want to use any extra GET parameters (?parameter=value) in the dropdown links and
have POST action instead of GET action when you click on them?


Kyle comments:

I never thought about masking each query string variable like Arnav Joy did, my concern with query strings had been passing the parameter values directly but his way did the trick.