0

I’m using the following code to hide certain posts by post ID on the front page (on https://michaelkummer.com/technology/howto-hide-posts-pages-in-wordpress/ and on the WordPress Developer Resource Page https://developer.wordpress.org/reference/hooks/pre_get_posts/)

function exclude_single_posts_home($query) {
     if ($query->is_home() && $query->is_main_query()) {
          $query->set('post__not_in', array(7, 11));
      }
}

What I would like to do is change it so that any new posts based on publish date (eg. posts published today) are hidden from the home page.

There's another code on the Wordpress Developer Resources page that makes it only displays search results after a specific date, but unfortunately I've had no luck adjusting it for my purpose using my very limited coding skills :(

function date_search_filter($query) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if ( $query->is_search ) {
            $query->set( 'date_query', array(
                array(
                    'after' => 'May 17, 2019', 
                )
            ) ); 
        }
    }
}
add_action( 'pre_get_posts', 'date_search_filter' ); 

Thank you very much for any assistance!

0

You could try do something similar to the code you have tried, but using the before argument instead of the after:

$query->set( 'date_query', array(
    array(
        'before' => '-1 day', 
    )
 ) );

This way, you should get only posts published before 1 day ago.

| improve this answer | |
  • Thank you! This seems to work well. – distinctly Apr 9 at 10:13

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.