1

I'm using this code to archive some custom post type :

$where .= " AND wp_postmeta.meta_value >= now()";

The metavalue is a date field. On the day of the value of the datefrom has been setup the post gets archive.

I would like it to be stored as archive 1 day after the meta date.

Is there any way to change

$where .= " AND wp_postmeta.meta_value >= now()";

By +24 hours instead of now? If yes, how is that possible?

Below is the complete function:

 function custom_posts_where( $where, $query ) {
    if (is_singular()) return $where;

    if (($query->query_vars['post_type'] == 'events' || (is_array($query->query_vars['post_type']) && $query->query_vars['post_type'][0] == 'events'))) {
        $year = 0;
        $month = 0;
        if (preg_match('@/([0-9]{4})/([0-9]{1,2})/@', $_SERVER['HTTP_REFERER'], $refer_match)) {
            $year = $refer_match[1];
            $month = $refer_match[2];

        }
        else if (preg_match('@/([0-9]{4})//@', $_SERVER['HTTP_REFERER'], $refer_match)) {
            $year = $refer_match[1];
        }

        if (is_archive()) {
            if (!is_month() && !is_year()) {
                $where .= " AND wp_postmeta.meta_value >= now()";
            }
            else {
                $where = preg_replace("@AND.*?\) \)@i", '', $where);
                if (is_month() || is_year()) {
                    $where .= " AND YEAR(wp_postmeta.meta_value) = " . $query->query_vars['year'];
                }
                if (is_month()) {
                    $where .= " AND MONTH(wp_postmeta.meta_value) = " . $query->query_vars['monthnum'];
                }
            }
        }

-- EDIT --

Below is the full set of functions to archive the post from the meta value:

/* ############################################################################### */
/* ########## START: these are the problem functions ############# */

function custom_posts_orderby( $orderby, $query ) {
    if (is_singular()) return $orderby;

    if (($query->query_vars['post_type'] == 'events' || (is_array($query->query_vars['post_type']) && $query->query_vars['post_type'][0] == 'events')) && !is_month()) {
        if (is_archive()) {
            if (!isset($query->query_vars['orderby']) || $query->query_vars['orderby'] == '_simple_fields_fieldGroupID_1_fieldID_5_numInSet_0') {
                $orderby = "wp_postmeta.meta_value ASC";
            }
        }

        if (stripos($orderby, 'post_title') !== false) {
            $orderbys = explode(" ", $orderby);
            $orderbys[0] = "trim(" . $orderbys[0] . ")";
            $orderby = implode(" ", $orderbys);
        }
    }

    $year = 0;
    $month = 0;
    if (preg_match('@/([0-9]{4})/([0-9]{1,2})/@', $_SERVER['REQUEST_URI'], $refer_match)) {
        $year = $refer_match[1];
        $month = $refer_match[2];
    }
    else if (preg_match('@/([0-9]{4})//@', $_SERVER['REQUEST_URI'], $refer_match)) {
        $year = $refer_match[1];
    }

    if ($year > 0 || $month > 0) {
        $orderby = "wp_postmeta.meta_value ASC";
    }

    return $orderby;
}

function custom_posts_join( $join ) {
    global $wp_query, $wpdb;
    if (is_singular()) return $join;

    if (($wp_query->query_vars['post_type'] == 'events' || (is_array($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'][0] == 'events')) && !is_month() && stripos($join, 'postmeta') === false) {
        if (is_archive()) {
            $join .= " INNER JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id";
        }
    }

    return $join;
}

function custom_parse_query( $query ) {
    if ($query->query_vars['post_type'] == 'events' || (is_array($query->query_vars['post_type']) && $query->query_vars['post_type'][0] == 'events')) {
        if (is_archive()) {
            $query->query_vars['meta_key'] = '_simple_fields_fieldGroupID_1_fieldID_5_numInSet_0';
            $query->query['meta_key'] = '_simple_fields_fieldGroupID_1_fieldID_5_numInSet_0';
        }
    }
}

function custom_posts_where( $where, $query ) {
    if (is_singular()) return $where;

    if (($query->query_vars['post_type'] == 'events' || (is_array($query->query_vars['post_type']) && $query->query_vars['post_type'][0] == 'events'))) {
        $year = 0;
        $month = 0;
        if (preg_match('@/([0-9]{4})/([0-9]{1,2})/@', $_SERVER['HTTP_REFERER'], $refer_match)) {
            $year = $refer_match[1];
            $month = $refer_match[2];

        }
        else if (preg_match('@/([0-9]{4})//@', $_SERVER['HTTP_REFERER'], $refer_match)) {
            $year = $refer_match[1];
        }

        if (is_archive()) {
            if (!is_month() && !is_year()) {
                $where .= " AND wp_postmeta.meta_value >= now()";

            }
            else {
                $where = preg_replace("@AND.*?\) \)@i", '', $where);
                if (is_month() || is_year()) {
                    $where .= " AND YEAR(wp_postmeta.meta_value) = " . $query->query_vars['year'];

                }
                if (is_month()) {
                    $where .= " AND MONTH(wp_postmeta.meta_value) = " . $query->query_vars['monthnum'];
                }
            }
        }

        else {
            if (!is_search || is_home() ) {
                $where .= " AND wp_postmeta.meta_value >= now()";
            }
            #} else {
                #$where .= " AND (wp_postmeta.meta_value = '_simple_fields_fieldGroupID_1_fieldID_5_numInSet_0' AND wp_postmeta.meta_value >= now())";
                #meta_key   '_simple_fields_fieldGroupID_1_fieldID_5_numInSet_0'
                #meta_value '2013-10-28'
            #}
        }
    }
    return $where;
}

function custom_posts_exclude( $query ) {
    # do we need to put some stuff here?
}

# only affect the front-end pages only, not the admin
if ((!is_admin() || ( isset($_REQUEST['action']) && $_REQUEST['action'] == 'awpqsf_ajax'))) {
    //if (!$_GET['post_type_index']) {
        add_filter('posts_orderby', 'custom_posts_orderby', 10, 2);
        add_filter('posts_join',    'custom_posts_join');
        add_filter('parse_query',   'custom_parse_query');
        add_filter('posts_where',   'custom_posts_where', 10, 2);
        add_action('pre_get_posts', 'custom_posts_exclude' );
    //}
}
0

This is actually a MySQL question and not related to WordPress but you can replace now() with NOW() + INTERVAL 1 DAY

  • Didn't change anything unfortunately :( :( any other tips ? – tibewww Jun 9 '16 at 13:16
  • The function I stated is the MySQL function to add 1 day to now() First of all make sure to also select on the meta key and check how the dates are actually stored, maybe it's not even targatable with a MySQL datetime function – Jeffrey von Grumbkow Jun 9 '16 at 13:22
  • the date is stored to a meta key: _simple_fields_fieldGroupID_1_fieldID_5_numInSet_0 Via simplefields and gravity form. Does this hep ?? I don't have any knowledge of SQL unfortunately :( Thanks for your time, really appreciate ! – tibewww Jun 9 '16 at 13:24
  • I've edit the post with the all archive system function in case it helps . . – tibewww Jun 9 '16 at 13:31
  • Shame on me, I just add to repalce the 1 by -1 ! Thx !!! – tibewww Jun 9 '16 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.