WordPress – single.php called twice

I am working on a plugin and for some quick and dirty development I have added a function call to single.php that calls a function in my plugin which stores data about the current post in the db.

I have noticed, though, that it is always called twice: once for the post I am requesting and once for another post (which does not show up in the content of the page).

Read More

Does anyone know why this is and, if so, whether it’s desired behaviour and how I can fix it if not?

Here’s an example similar to my single.php:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                   <?php do_action('msw_single_php', $wp_query);  ?>

And one similar to my plugin file:

add_action('msw_single_php', 'msw_sp', 0, 1);
function msw_sp(WP_Query $query) {
    global $wpdb;
    $wpdb->insert($wpdb->prefix . 'msw_career_view_debug', array(
        'request_info'      => json_encode($query),
    ));
}

Related posts

Leave a Reply

2 comments

  1. I ran into the same issue and now it’s SOLVED.

    For me, the issue only happened on Firefox, when single.php was called. And the post which you didn’t want was the adjacent one.

    I then searched a lot and finally found the solution here:

    http://shinephp.com/single-php-called-twice-how-to-stop/

    which gives another reference to:

    http://wordpress.org/support/topic/singlephp-called-for-current-and-next-post-with-pretty-permalinks

    Thanks to the above links.

    I’d like to put a brief here:

    The reason is Firefox prefetch feature. For single.php, WP automatically puts two <link> in <header>, and one of them is rel=”next”, which points to the next post (adjacent post).

    When Firefox sees this, it’ll prefetch the page pointed to by the <link> in order to speed up in case you go to the next post after reading the current one. And thus that post will be called but you will never see it displayed. This is also why you can’t see any message you dump for debugging, not even backtrace.

    BTW the action which adds the two <link> is added in “default-filters.php”. For WP 3.5.1 it’s in line #204.

    Solution is:

    Write a function (for example “remove_adjacent_links”) which executes:

    remove_action(‘wp_head’, ‘adjacent_posts_rel_link_wp_head’)
    

    and hook the function (remove_adjacent_links) to “init”.

    This way, the links will be removed from .

    However if you do want the links to be there, maybe you’ll have to leave it this way and tolerate Firefox’s prefetch… this is what I’m doing because it doesn’t matter for my case. I didn’t try whether there is a way to leave the there and use WP code to stop Firefox from prefetching.

  2. I suggest you start debugging yourself, you do have WP_DEBUG on?
    var_dump($query) in msw_sp() check which posts get passed, and where are they on the page?
    I also suggest you var_dump(is_single()) to check if it is a single page.
    var_dump($query->is_main_query()) will tell if it is the page building query or an extra query.

    Is the second post somewhere in a sidebar?

    Not a real answer but a way to get you started.