0

I've been wrestling with some code cobbled together for a function I found, first published circa 2009 and my inexperience with PHP has me at a loss.

The function is designed to count episodes in a podcast category, thus:

function Get_Post_Number($postID){
$temp_query = $wp_query;
$postNumberQuery = new WP_Query(array (
'orderby' => 'date',
'order' => 'ASC',
'post_type' => 'any',
'category_name' =>'podcast-episodes',
'posts_per_page' => '-1'
));
$counter = 1;
$postCount = 0;
if($postNumberQuery->have_posts()) :
    while ($postNumberQuery->have_posts()) : $postNumberQuery->the_post();
        if ($postID == get_the_ID()){
            $postCount = $counter;
        } else {
            $counter++;
        }
endwhile; endif;
wp_reset_query();
$wp_query = $temp_query;
return $postCount;
}

As far as I can tell, this works fine. I created a shortcode to return the value in the query, which then resulted in the excerpt not showing. I then created a filter to do the same and the result was the same. No excerpt. Here I'll just post the shortcode:

/**
* Create shortcode to display Episode count.
*/

function episodeNo() {
    $epID = get_the_ID();
    $epNumber = Get_Post_Number($epID);
    echo 'Episode ' . $epNumber;
    }
add_shortcode('episode','episodeNo');

Here is my query:

$args = array(
                    'posts_per_page'        => 3,
                    'category_name'         => 'podcast-episodes'
                );  
$query = new WP_Query( $args );
                // The Loop
                if ( $query->have_posts()){
                    echo '<ul class="entries">';
                    while ( $query->have_posts() ) {
                        $query->the_post();
                        echo '<div class="entries-post">';
                        echo '<a href="' . get_permalink() . '">';
                        echo '<h1 class="entries-title">' . get_the_title() . '</h1>';
                        echo '</a>';
                        do_shortcode('[episode]');
                        echo '<div class="entries-excerpt">';
                        the_excerpt();
                        echo '</div>';
                        echo '</div>';
                        }
                    }
                    echo '</ul>';

                }
                /* Restore original Post Data */
                wp_reset_postdata();

Now, if the shortcode runs before the_excerpt, it correctly displays the episode number but no excerpt.

Title

Episode 11

Title

Episode 10

Title

Episode 9

If it runs after the the_excerpt, it correctly displays the excerpt and the episode number:

Title

This week I cry into my coffee

Episode 11

Title

This week I approach a new design project

Episode 10

Title

This week I mostly play video games

Episode 9

Unfortunately, the design is such that episode must be displayed before excerpt.

The exact same occurs were I to use a filter that performed the same duty as the shortcode.

Does anyone have any idea why the_excerpt fails to display if called after this shortcode? I'm certain it's the way in which the episode number is calculated or returned, but know too little to correct it myself. Believe me, I have attempted to. At length.

0

It seems you need to rest the post data before return the $postCount. Before return it, add the follow:

wp_reset_postdata();

--

There is a difference between wp_reset_query()and wp_reset_postdata(), as explained here.

  • wp_reset_query() - ensure that the main query has been reset to the original main query.
  • wp_reset_postdata() - ensures that the global $post has been restored to the current post in the main query.
  • Unfortunately, this doesn't change anything, assuming you mean: ' } endwhile; endif; wp_reset_query(); $wp_query = $temp_query; wp_reset_postdata(); return $postCount; }' – Killinski Jun 23 '16 at 2:20
  • It's showing the the excerpt from the last post queried by the shortcode. You need to setup_postdataagain after the shortcode so wordpress understand it's back to the first loop. In the loop, after the do_shortcode and before the the_excerpt();, add the following setup_postdata($query->post);. – cbonomini Jun 23 '16 at 2:51
  • Thanks so much for the extra assistance here. That last piece solved it. – Killinski Jun 23 '16 at 14:27
0

I think you can simplify your code a lot, by just using:

$episode_number = ( $query->found_posts - $query->current_post );

in the loop as the episode's number.

So you don't need that problematic Get_Post_Number() function and there's no need to use a special shortcode for this.

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.