0

I have a static front page that checks if the user is logged in, and if he/she is a number of x posts from a specific category is displayed. For that I was using this code:

query_posts('cat=2&showposts=5'.'&orderby=date&order=desc');
while (have_posts()) : the_post();
the_content();
endwhile;

Now I found out that's not right to use query_post and that I should use pre_get_posts instead. I've tried to do this but somehow my page breaks down saying that my server is configured incorrectly.

3
  • Please post your exact error message and what you've tried. The above code btw shows nothing that could be used to check if the user is logged in.
    – kaiser
    Commented Apr 26, 2013 at 14:40
  • I didn't said that the code above checks if the user is logged in. I have another code for that. This code just shows to the logged in users a a numbers of posts from a certain category. That's all . What I want is to convert the query_posts into pre_get_posts. I don't need a code to check if the user is logged in or smth.
    – Villalobos
    Commented Apr 26, 2013 at 14:45
  • and my error was from removing the above query_posts and inserting another code for pre_get_posts into functions.php
    – Villalobos
    Commented Apr 26, 2013 at 14:48

1 Answer 1

2

Without seeing the broken code it is hard to say, but your filter should look something like this:

function pregp_wpse_97354($qry) {
  if (is_front_page() && is_main_query() && is_user_logged_in()) {
    $qry->set('cat',2);
    $qry->set('posts_per_page',5);
    $qry->set('orderby','date');
    $qry->set('order','DESC');
  }
}
add_action('pre_get_posts','pregp_wpse_97354');

You mentioned something about the user being logged in, which your code does not check for, but I threw that in. This will run before the theme templates load so if this is conditional on the user being logged in you need to include that check in the filter callback.

1
  • Thanks. I think this will do the work. Anyway what I was saying is that I have a static front page with a login form. If you are logged in or you log in on same page you see the posts that I was retrieving with that function.
    – Villalobos
    Commented Apr 26, 2013 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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