Home › Forums › MapPress Support › Mashup with date filter
Tagged: date, filter, mappress, mashup, query_posts
- This topic has 3 replies, 2 voices, and was last updated 10 years, 9 months ago by
BradSiegfried.
-
AuthorPosts
-
December 5, 2012 at 1:46 am #11020
urbelab
ParticipantCan anyone help me with a query_posts to filter the post of the map in a certain amount of days?
For example I want to show only the maps of posts published in the last week. It’s possible?
December 6, 2012 at 3:24 am #11029BradSiegfried
ParticipantDecember 23, 2012 at 11:59 pm #11216urbelab
Participantthanks BradSiegfried
I had already read the link you gave me but I would like, if it’s possible, to know how to set the shortcode within a page.Could you help me?
December 29, 2012 at 7:04 am #11245BradSiegfried
ParticipantI think you’ll need to write some PHP since you want a date range calculated based on the current date. When I need to write shortcodes, I keep them in a utility plugin so I don’t have to worry about theme changes. You can learn a lot by looking at the plugins that other people have written. See http://codex.wordpress.org/Writing_a_Plugin for some help getting started.
You might do something along the lines of the code below to filter by date:
global $my_datefrom, $my_dateto;
// Create a new filtering function that will add our where clause to the query
function my_filter_where($where = ”) {
global $my_datefrom, $my_dateto;
if ($my_datefrom) $where .= ” AND post_date >= ‘” . $my_datefrom;
if ($my_dateto) $where .= “‘ AND post_date <= ‘” . $my_dateto . “‘”; else $where .= “‘”;
return $where;
}// [my_querymap my_datefrom=”2012-07-01″ my_dateto=”2012-12-02″ ]
function my_querymap($atts, $content = null) {global $my_datefrom, $my_dateto;
extract( shortcode_atts( array(
‘my_datefrom’ => date(‘Y-m-d’, strtotime(‘-30 days’)),
‘my_dateto’ => date(‘Y-m-d’),
), $atts ) );
$result = “”;// Do the query to return desired posts for my_datefrom to my_dateto:
$my_query_string = array(‘category_name’ => ‘my_categoryname’, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, ‘posts_per_page’ => ‘-1’);
// Add a filter that will add our where clause to the query
add_filter(‘posts_where’, ‘my_filter_where’);
$my_query = new WP_Query( $my_query_string );
$my_news = $my_query->get_posts();
remove_filter(‘posts_where’, ‘my_filter_where’);… more code would go here to create the map from the query results …
I hope that helps. If not, maybe Chris can suggest an easier way to do what you want.
Brad
-
AuthorPosts
- You must be logged in to reply to this topic.