• smerriman

    (@smerriman)


    I have a shortcode which runs a custom query, then calls wp_reset_postdata(), which resets the $post variable back to its original value.

    Inside the relevanssi_do_excerpt function, there is a hack which sets $post to the post to generate an excerpt for.

    When $content = apply_filters( 'the_content', $content ); is run inside this function, the shortcode executes. However, wp_reset_postdata doesn’t do anything, because you’re not inside a properly set up query. This means, after this line, the $post variable is set to a completely different item (the last one used by the executed shortcode).

    This causes all sorts of problems, but the particular one is that the relevanssi_excerpt_content filter is then called with a completely wrong $post parameter.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mikko Saari

    (@msaari)

    You basically should never call wp_reset_postdata() outside a template loop. Custom queries should only be used in templates.

    If you want to just get some posts, instead of doing a custom query you should use get_posts(). It does the same thing, is actually easier to use than WP_Query and doesn’t affect anything outside your shortcode function, so no danger of breaking things up.

    That would be the easiest way to fix this, and you’d also avoid future problems.

    Thread Starter smerriman

    (@smerriman)

    I *am* using get_posts(), not WP_Query, of the form:

    $results = get_posts(..); foreach ($results as $post) { setup_postdata($post); /* output stuff */ } wp_reset_postdata();

    This is standard coding practice. This *should* always be called inside the loop – it is the plugin which is calling it outside the loop which is causing the problem.

    Thread Starter smerriman

    (@smerriman)

    Look at the top of the relevanssi_do_excerpt function

    	// Back up the global post object, and replace it with the post we're working on.
    	$old_global_post = null;
    	if ( null !== $post ) {
    		$old_global_post = $post;
    	}
    	$post = $t_post; // WPCS: override ok, must do because shortcodes etc. expect it.

    Yes, shortcodes and the_content etc expect it – while hacking the post object like this solves some issues, it’s not the proper way to do it in WordPress. You need to be in a proper loop in order to use the_content and have it function correctly, otherwise you run into issues like the one I mentioned.

    Plugin Author Mikko Saari

    (@msaari)

    Yeah, I know it’s not the proper way to do it –?but unfortunately there’s no proper way to do what Relevanssi is doing, so the options are – as far as I can tell – to do it this way or not to do it at all.

    Add your shortcode to the list of shortcodes Relevanssi disables in excerpt-building, that should avoid the problem.

    add_filter( 'relevanssi_disable_shortcodes_excerpt', function( $shortcodes ) { $shortcodes[] = 'your-shortcode'; return $shortcodes; } );

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Excerpt functions break when a shortcode calls wp_reset_postdata’ is closed to new replies.