0
votes

I have a Wordpress function that is used within two pages of my site, home page and category archive. Whilst this code works perfectly on the category archive, the home page seems to have issues with wp_reset_postdata() - but i don't know how to fix it. have tried wp_reset_query() but it doesn't seems to do anything different.

This code below outputs a list of the posts within a specific category (for this example, i'm just outputting the IDs). I have a separate query that checks if a post has been 'pinned'. If it has, it nests in position 1 of the query (so effectively inputting a post into the array output)

if ( $wpb_all_query->have_posts() ) :

                while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
                    if( $count === 1 ) {
                        if ( $pinnedID ) {
                            while ( $wpb_pinned_content_query->have_posts() ) : $wpb_pinned_content_query->the_post();
                                echo $count . ": " . get_the_ID();
                                echo "<br/>";
                            endwhile;
                            wp_reset_postdata();
                                echo $count . ": " . get_the_ID();
                                echo "<br/>";
                        }
                        else {
                            echo $count . ": " . get_the_ID();
                            echo "<br/>";
                        }
                    }
                    else{
                        if($pinnedID === get_the_ID()){
                        }
                        else{
                            if ( $pinnedID ) {
                                if( $count === 0 || $count === 8 ) {
                                    echo $count . ": " . get_the_ID();
                                    echo "<br/>";
                                } 
                                else {
                                    echo $count . ": " . get_the_ID();
                                    echo "<br/>";
                                }
                            }
                            else{
                                if( $count === 0 || $count === 9 ) {
                                    echo $count . ": " . get_the_ID();
                                    echo "<br/>";
                                } 
                                else {
                                    echo $count . ": " . get_the_ID();
                                    echo "<br/>";
                                }   
                            }
                        }   
                    }                   
                    $count ++;
                endwhile;
                wp_reset_postdata();
endif;

if there are no 'pinned' posts, the following is outputted

0: 13402
1: 13383
2: 13409
3: 13397
4: 13361
5: 13332
6: 10886
7: 10884
8: 10862
9: 10795

if there is a 'pinned' post this is outputted;

0: 13402
1: 10619
1: 171 <- this is the ID of the homepage - it should be ID #13383
2: 13409
3: 13397
4: 13361
5: 13332
6: 10886
7: 10884
8: 10862
9: 10795

I know it's to do with the wp_reset_postdata() - but i don't know how to fix it. Does anyone have any ideas?

1

1 Answers

0
votes

I've managed to figure this one out myself

if ( $pinnedID ) {
    $backup=$post;
    $wpb_pinned_content_query = new WP_Query($innerargs);
    while ( $wpb_pinned_content_query->have_posts() ) : $wpb_pinned_content_query->the_post();
        echo $count . ": " . get_the_ID();
        echo "<br/>";
    endwhile;
    $post=$backup;
    if($pinnedID !== get_the_ID()) {
        echo $count . ": " . get_the_ID();
        echo "<br/>";
    }

}

By declaring the secondary query inline then storing the global $post into a backup variable - i was able to access the original $post again without having to call wp_reset_postdata();