Mashup with date filter

Home Forums MapPress Support Mashup with date filter

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #11020
    urbelab
    Participant

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

      #11029
      BradSiegfried
      Participant
        #11216
        urbelab
        Participant

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

          #11245
          BradSiegfried
          Participant

            I 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

          Viewing 4 posts - 1 through 4 (of 4 total)
          • You must be logged in to reply to this topic.