• dantina

    (@dantina)


    Hi,

    I’ve upgraded my site to version 4.0 and trying to add the code snippet below on 2 pages on a new website I’ve just started but it only works on one of the pages, returning the latest 10 posts. On the second page, I wanted only the latest 3 posts, but have_posts() function seem to return false and not going into the if statement – yet there are posts in the blog.

    if ( have_posts() ) :
    // get the latest 10 …
    $posts = get_posts(‘numberposts=10&order=DESC&orderby=post_date’);
    endif;

    I’ve tried rewind_posts() and wp_reset_postdata() functions after each call and it hasn’t worked either. I’ve searched through the posts and found some references to permalinks but I wasn’t using permalinks so unsure what the problem is (I tried using permalinks and that doesn’t work either).

    Why is the same code only working on one page? Does anyone have a clue, please? I’ve got this at the top of both pages:

    <?php
    /* include wordpress blog contents */
    define(‘WP_USE_THEMES’, false);
    require(“/path/to/wp-blog-header.php”);
    ?>

    The blog site is here: https://www.totalpersoninu.co.uk/ but I’m trying to display the posts on these pages: https://www.balancewellness.co.uk/zNEWsite/index.php and https://www.balancewellness.co.uk/zNEWsite/blog/index.php. It works fine on the latter but not on the former – the posts should be displayed under the section titled “Latest information / events”.

    Thanks for your help.
    Dantina

Viewing 5 replies - 1 through 5 (of 5 total)
  • Just guessing here but it sounds like your page is outside the WP environment and your calling WP via the require. If so, then at this point you won’t have any posts, there’s no query running yet to get posts.

    Once you call get_posts(), there would be a query, but that function never runs b/c it’s inside the if ( have_posts() ) which is false.

    So, I think, just run get_posts() w/o the if surrounding it and you’ll have some posts.

    Thread Starter dantina

    (@dantina)

    Hi Heavox,

    Thanks for your response but I am assuming the have_posts() function is supposed to check if there’s any post using some in-built query; that way, you could do something else if there’s no post. I am following the instructions here: https://codex.www.ads-software.com/Integrating_Wordpress_with_Your_Website – which uses the codes in the same way to get posts from outside WP environment.

    Besides, I am doing exactly the same thing on the other page and it works, so I don’t think that’s the problem.

    Dantina

    Dantina, you are correct have_posts() checks if there are any posts based on a query. Could it be that the page where it words is INSIDE WordPress, so does have posts (based on built-in-query), and the page where it doesn’t work is OUTSIDE WordPress so has no query yet, thus, has no posts (so your get_posts() never runs)?

    Notice, on the Codex page you site, the get_posts() function is NOT inside an if ( have_posts() conditional statement:
    https://codex.www.ads-software.com/Integrating_Wordpress_with_Your_Website#Examples

    Thread Starter dantina

    (@dantina)

    Good point about the examples used! I had actually taken some ideas from https://codex.www.ads-software.com/The_Loop_in_Action – to try and check if there’s post first and then do something on the basis of the result.

    Thanks for your help. I’ve taken get_posts() outside the if statement and that did work. I guess I am confused because both pages are OUTSIDE WordPress and since it is the same code, I wasn’t sure why it worked on one page and not the other.

    Dantina

    Hey, @dantina, please mark this topic as resolved (if, indeed, it is resolved): Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘have_posts() not working’ is closed to new replies.