Title: code snippet for no-posts display message
Last modified: February 16, 2019

---

# code snippet for no-posts display message

 *  Resolved [webbuildermn](https://wordpress.org/support/users/webbuildermn/)
 * (@webbuildermn)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/code-snippet-for-no-posts-display-message/)
 * Hi, I was wondering if I am even on the right track with this wordpress function.
   I want to put it in code-snippets so it exists across themes.
 * The issue is I am making a website for a client-friend. He wants a private blog
   that only registered users can view the posts of, and his homepage to display
   his latest posts. Since all posts are “private”, if the visitor is not logged
   in, no posts show, and with Illustratr, we get a “not found” message, which I
   edited to say “You are not logged in. Please log in to view all posts”. With 
   Astra, a theme I want to switch to, we get a white screen in the middle (unless
   logged in, in which case we get an awesome display of posts). But this question
   is not about themes- because I already asked a more general question to the Astra
   forum. This question is about how to write a custom function/hook, and whether
   that is what I need to do. I worked out the following code, but it didn’t work
   out. Am I even on the right track?
 *     ```
       function no_posts_on_home () {
       	if ( is_home() && !have_posts() ){
       		return ?> <h1>You are not logged in. Please log in to view all posts.</h1> <?php ); 
       	}
       }
   
       add_action('pre_get_posts', 'no_posts_on_home ()');
       ```
   
 * Then I changed the return line to just be
    echo “You are not logged in” ;
 * I got this error:
    Warning: call_user_func_array() expects parameter 1 to be 
   a valid callback, function ‘no_posts_on_home ()’ not found or invalid function
   name in /app/public/wp-includes/class-wp-hook.php on line 286
 * If there’s a better place to ask this, let me know!
    Thanks for your help

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/code-snippet-for-no-posts-display-message/#post-11213579)
 * No, your code is not at all what you need. What it says is “before you look for
   posts, if it’s blog page and there are no posts, leave the function and print
   that you’re not logged in”. and the parameter to the add_action has parentheses,
   which it shouldn’t.
 * The problem you are having is because of the method chosen to hide the posts.
   Marking them private is not a good way to indicate when to show them, because
   they are only visible to those with the Editor or Admin role (unless you change
   that).
    You would be better off with a plugin like [https://wordpress.org/plugins/wp-force-login/](https://wordpress.org/plugins/wp-force-login/)
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/code-snippet-for-no-posts-display-message/#post-11214142)
 * “pre_get_posts” is the wrong track to change the not found message. At the time
   pre_get_posts happens, the query has not executed and we don’t yet know if anything
   was found or not.
 * The not found message comes from the theme template. To change it, you likely
   need to change the theme template, unless the theme offers a filter for this 
   message (would be unusual). When you alter theme templates, if it’s a theme subject
   to updates, you should create a [child theme](https://codex.wordpress.org/Child_Themes)
   to contain altered templates.
 * If you are going to alter templates, one way to prevent non-logged in users from
   seeing posts is to include `is_user_logged_in()` in the logic to run the loop.
   Consider carefully the best logic structure for this. IMO, simply changing the
   not found message is sub-optimal because logged in users making bad requests 
   will be given the wrong message. Ideally, you want a not found message for not
   found posts when logged in and a please login message for those not logged in,
   regardless of posts found.
 *  Thread Starter [webbuildermn](https://wordpress.org/support/users/webbuildermn/)
 * (@webbuildermn)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/code-snippet-for-no-posts-display-message/#post-11216608)
 * That plug in work brilliantly thank you so much. It also solved another minor
   issue I had.
 * I guess I was out of my depth in terms of coding. I did try to change the theme
   template to no avail. But the plug-in solved it anyway. As they usually do.
 * When I first made the blog for him I used illustrator and I did some of customization.
   We kind of got stuck on that team. Now I use code snippets for the few things
   that are necessary and plugins I like the one you told me about.
 * Thank you

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘code snippet for no-posts display message’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 3 participants
 * Last reply from: [webbuildermn](https://wordpress.org/support/users/webbuildermn/)
 * Last activity: [7 years, 3 months ago](https://wordpress.org/support/topic/code-snippet-for-no-posts-display-message/#post-11216608)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
