• marcelomuraro

    (@marcelomuraro)


    Hello everybody. I need some help from you all.

    I’m using “custom post types” to develope an “agenda” for my costumer. It’s too simple. It has some fields like “name”, “date”, “hour”, “where” and “date_limit” (I don’t want to show old items in agenda)

    So, I would like to index the records by “date_limit” and filter the records excluding only the oldest.

    Is it possible? How can I do this?

    Best regards!

Viewing 2 replies - 1 through 2 (of 2 total)
  • This is untested sample code, it works with posts not tried with custom post types, if it helps post the solution back to pastebin, to help others.

    Line 2 sets the number of days, this should filter out older than 28 days!

    /* Function to return a date filter */
    function dr_filter_where( $where = '') {
            $days = 28;
            $where .= " AND post_date > '" . date('Y-m-d', strtotime('-' .$days .' days')) . "'";
            return $where;
    }
    add_filter( 'posts_where', 'dr_filter_where' );
    /*
    Add the filter and remove it after the WP_Query see pastebin
    */

    HTH

    David

    Thank you! The removing of the filter must have been the important step keeping what I had previously from working. I used the below filter to return every custom post type older than a year.

    /*Function to filter out post less than a year old */
    function filter_where($where = '') {
    		  $today = date('Y-m-d');
    		  $newdate = strtotime ( '-1 year' , strtotime ( $today ) ) ;
    		  $ayearago = date( 'Y-m-d' , $newdate );
    		  $where .= " AND post_date <= DATE('{$ayearago}')";
    		  return $where;
    		}
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘indexing on custom post type field’ is closed to new replies.