0

The problem: infinite loop works only when the user is logged and then it shows posts that were not published yet. Once I log out, the infinite scroll doesn't work anymore.

The setup: I have the following custom infinite loop function:

add_action('wp_ajax_zephyr_infinite_scroll', 'zephyr_get_nextpage');  

function zephyr_get_nextpage() {
    $loopFile = $_POST['loop_file'];
    $paged = $_POST['page_no'];
    $vv = stripslashes($_POST['vars']);
    $vars = json_decode($vv, true);
    $vars['paged'] = $paged;
    global $isms;
    global $zephyr_post_columns;
    $zephyr_post_columns = $vars['columns'];
    unset($vars['columns']);
    $isms = $_POST['mas'];
    query_posts($vars);
    global $wp_query; 
    if ( $wp_query->found_posts == 0 ) { 
        echo 0;
    } else {
        get_template_part( 'templates/'.$loopFile );
    }
    exit;  
}

Then I have in my loop.php file:

<?php global $zephyr_cont; 
    global $zephyr_post_layout;
    global $zephyr_i;
    global $isms;
    global $zephyr_post_columns;
    $zephyr_i = 0;
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <?php 
        if ( $isms == 'true' ) {
            $zephyr_i++;
            get_template_part( 'templates/post', 'masonry'.get_post_format() );
        } else {
            get_template_part( 'templates/post', get_post_format() );
        }
    endwhile; else : ?>
        <h2><?php _e('No posts found, go ', 'zephyr'); ?> <a href="<?php echo home_url(); ?>" title="<?php bloginfo('name'); ?>"><?php _e('home', 'zephyr'); ?></a>?</h2>
    <?php endif; ?>

And the script.js file:

function loadArticle(pageNumber) {
        $('#ajaxload').fadeIn(300);
        $.ajax({  
            url: Zephyr.ajaxurl,  
            type:'POST',  
            data: {
                action : 'zephyr_infinite_scroll',
                page_no : pageNumber,
                loop_file : 'loop',
                vars : Zephyr.queryvars,
                mas : isms
            },   
            success: function(html){
                if ( html !== '0' ) {
                    if ( isms ) {
                        $zephyr_container.isotope( 'insert', $(html) );
                    } else {
                        $(html).hide().appendTo('#content').fadeIn('300');
                    }
                    layoutSidebar(ms);
                } else {
                    noload = 1;
                }
                $('#ajaxload').fadeOut(300);
            }  
        });  
        return false;  
    }
4

add this before your other add_action call:

add_action('wp_ajax_nopriv_zephyr_infinite_scroll', 'zephyr_get_nextpage');

the wp_ajax_ filter runs only when you are logged in. wp_ajax_nopriv_ runs only when logged out. This is so you can separate your requests, and maybe treat them differently, if that's the case.

Check wp_ajax_(action)

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.