• Resolved bba01

    (@bba01)


    Hi!

    I’m trying to fetch the ranking with a short code to be able to sort in the Tables by supsystic plugin:

    function grab_post_meta_shortcode_fn( $atts ) {
    $atts = extract( shortcode_atts( array(
    ‘post_id’ => false,
    ‘key’ => ”
    ), $atts ) );
    if ( ! $key ) {
    return;
    }
    $post_id = (int)($post_id === false ? get_the_ID() : $post_id);
    $data = get_post_meta( $post_id, $key, true );

    if ( $data ) {
    return ‘<span class=”grab-post-meta-value ‘ . sanitize_html_class(‘id-‘ . $post_id) . ‘ ‘ . sanitize_html_class(‘key-‘ . $key) . ‘”>’. $data .'</span>’;
    }
    }
    add_shortcode( ‘grab_post_meta’, ‘grab_post_meta_shortcode_fn’ );

    I am displaying the star ratings in a table without a problem. However when I’m trying to get their numerical rating it doesn’t display anything. I’ve checked that I use the right page id and I know that the pages have a rating added because the stars are showing correctly. I use the short code like this: [grab_post_meta id=”1393″ key=”_glsr_ranking”]

    What am I missing here?

    Thank you for the best rating plugin out there!

    With best regards,

    bb

    The

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Gemini Labs

    (@geminilabs)

    It’s working for me:

    add_shortcode('grab_post_meta', function ($atts) {
        $pairs = [
            'key' => '',
            'post_id' => false,
        ];
        extract(shortcode_atts($pairs, $atts));
        $post_id = get_post($post_id)->ID;
        if ($data = get_post_meta($post_id, $key, true)) {
            return sprintf('<span class="grab-post-meta-value %s %s">%s</span>',
                sanitize_html_class('id-'.$post_id),
                sanitize_html_class('key-'.$key),
                $data
            );
        }
    });

    Here is the debug test:

    glsr_debug(do_shortcode('[grab_post_meta key=_glsr_ranking]'));
    

    And here is the result:

    Thread Starter bba01

    (@bba01)

    Hi and thank you for a swift reply!

    The shortcode works for me when I put it on the same page that it fetches the ranking from with the id provided in the shortcode. But when I move it to the parent page, still same page id as before, it doesn’t show anything.

    Page A, page id is 34, page not ranked.
    — Page B, page id is 1181, parent page is A, page is ranked.
    — Page C, page id is x, parent page is A, page is ranked

    When I put this on page A or C it doesn’t work: [grab_post_meta id=”1181″ key=”_glsr_ranking”]
    When I put this on page B it works: [grab_post_meta id=”1181″ key=”_glsr_ranking”]

    I can’t figure out what I’m doing wrong… can you?

    With best regards,

    bb

    Plugin Author Gemini Labs

    (@geminilabs)

    Shouldn’t it be [grab_post_meta post_id=1181 key=_glsr_ranking]

    Thread Starter bba01

    (@bba01)

    Yes it should. Thank you. Sorry for taking your time and thank you for a great plugin and an even better support! ?? Have a nice week!

    /bb

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Trying to fetch ranking with short code’ is closed to new replies.