I am looking to have a drop down menu at the top of a category page that will allow me to then filter the posts by date.
I will most probably have to make use of custom fields, but that isn't the issue.
I know you can make a custom post query using GET style variables, but with pretty URLs enabled, I cannot seem to use the GET variables to filter specific posts (e.g. www.domain.com/category/?orderby=title&order=ASC
etc etc )
I have tried looking for plugins, but nothing seems to jump out at me for what I need, and I have also noticed a lot of talk on here about similar subjects, with no decent solutions for my situation.
The general query would be this like :
<?php $posts = query_posts( $query_string . '&orderby=date&order=asc' ); ?>
<?php if( $posts ) : ?>
//whatever
<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
//whatever
<p><?php the_content(); ?></p>
<?php endforeach; ?>
<?php endif; ?>
For dropdown, you can do something like this :
$args = $args=array(
'cat' => $cat_id,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'DATE',
'order' => 'ASC' // or DESC
);
<form action="<? bloginfo('url'); ?>" method="get">
<select name="page_id" id="page_id">
<?php
global $post;
$args = array( 'numberposts' => -1);
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<? echo $post->ID; ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="view" />
</form>
And another option :
<?php
$cat_id = get_cat_ID('uncategorized'); //your-category
$args=array(
'cat' => $cat_id,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'DATE',
'order' => 'ASC' // or DESC
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
?>
<form name="jump">
<select name="menu">
<?php
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<option value="<?php the_permalink() ?>"><?php the_title(); ?></option>
<?php
endwhile;
}
?>
</select>
<input type="button" onClick="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;" value="Go">
</form>
<?php
wp_reset_query();
?>
i was looking for search posts by date. didn't able to found suitable solution on stackoverflow. i then printed the wp_query object and sorted this thing out. it worked for me. in my scenario m searching for posts by their title or by date. here is the code for the hook.
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
// check if query is a date
$search_query = $query->query['s'];
$date_format = DateTime::createFromFormat('d/M/Y', $search_query);
if ($date_format)
{
$dte = date('j',$date_format->getTimestamp());
$month = date('n',$date_format->getTimestamp());
$year = date('Y',$date_format->getTimestamp());
}
if (isset($dte) && isset($month) && isset($year)) {
unset($query->query['s']);
unset($query->query_vars['s']);
$query->query['date_query'] = array(
array(
'year' => $year,
'month' => $month,
'day' => $dte,
)
);
$query->set('date_query', array(
array(
'year' => $year,
'month' => $month,
'day' => $dte,
)
)
);
}
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
as u can notice this filter automatically checks if passed param is string or date. m using
$query->set('post_type', 'post')
to get results of post only other wise it will fetch page too.
suppose u have date under each post u can add href to that date. so to get all posts of that date
on the end of href add search param like ?s=something
http://my.blog?s=1/Jun/2015
and so on the template u dont need to write ur custom forwhile just use default template functions like have_posts()