0

I have custom post type named: easytask, and the following code is shortcode from plugin to retrieve data from the custom post above ( easytask ).

function easy_shortcode( $attr ) {

    // Shortcode parameter
    extract( shortcode_atts( array(
        'med' => -1,
        ), $attr ) );

    $easy_id = explode( ",", $med );

    // Get data from custom post type
    $easy_arg = array(
        'post__in' => $easy_id, 
        'post_type' => 'easytask',
    );

    $easy_query = new WP_Query( $easy_arg );

    if ( $easy_query->have_posts() ):

        while ( $easy_query->have_posts() ) : $easy_query->the_post();

            // Content from our custom Post goes here...

        endwhile;
        wp_reset_postdata();

    else:

        // If empty

    endif;

}

add_shortcode( 'easy-task', 'easy_shortcode' );

But when I put the shortcode into the blog list, the theme function that uses get_permalink return the wrong URL, in this case, the URL should be the return to the post URL itself, but my issue is the URL return the custom post URL.

NOTE: All posts in blog list have the correct link except the post that containing the easy-task shortcode.

The following code is the theme function I found in theme functions.php that conflict with the easy-task shortcode.

function adventure_posted_on() {

            printf( __( '<span class="%1$s">Last Updated:</span> %2$s <span class="meta-sep">by</span> %3$s', 'organic-adventure' ),
                'meta-prep meta-prep-author',
                sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
                    esc_url( get_permalink() ),
                    esc_attr( get_the_modified_time() ),
                    get_the_modified_date()
                ),
                sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
                    esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
                    sprintf( esc_attr__( 'View all posts by %s', 'organic-adventure' ), get_the_author() ),
                    get_the_author()
                )
            );
}

Can anyone help me with this?

  • please post the full code of the shortcode function. where in that function do you return the output? – Michael May 2 '17 at 5:46
  • @Michael : Even if I didn't return any data from the custom post still get the wrong permalink in the link that generated from theme function above – Peter Chimp May 2 '17 at 6:24
  • try and move the wp_reset_postdata() to after the endif. – Michael May 3 '17 at 0:27
0
  1. IMO It is rarely a good idea to evaluate shortcodes outside of the specific post page as too many times you need the context for either the evaluation itself or styling.

  2. Your problem is most likely related to the use of the_post and wp_reset_postdata. What is exactly the post being reset to in that case? A thumb rule, as long as you can avoid it, use explicit post ID parameters in function calls and try to avoid any assumption about which globals are being set to what. Unfortunately, some core api just do not work without the globals being set, and if you need to use them than you need need to find out what wp_reset_postdata does and maybe setup the "original" post data in a different way.

  • But i have to fix this issue from the plugin side as I am not that theme developer. My client just report that my plugin conflict with the theme. – Peter Chimp May 2 '17 at 7:00
  • Sorry, not sure how what you say is in any contradiction to the answer. if your plugin is the one which does the shortcode, then fix it. If you are just using it.... well if the shortcode is broken than there is not much you can do about it. – Mark Kaplun May 2 '17 at 19:55

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.