2

I am using a Visual Composer (Post Grid) element, with a custom template.

I'd like to output the post id via a shortcode, so I've created a simple shortcode:

   function myshortcode_title( ){
   return get_the_ID();
   }
   add_shortcode( 'page_title', 'myshortcode_title' );

but it doesn't seem to retrieve the post ID. I am adding it to the grid via a Text block.

The shortcode works on any other page, but not inside the VC post grid.

How else can I access the post ID inside the post grid?

Thanks in advance! Here is a screenshot of the element.

Here is a screenshot of the element

Micho
  • 3,929
  • 13
  • 37
  • 40
Adrian
  • 57
  • 1
  • 8

1 Answers1

0

I now it's late but I had the same problem and this answer can help other people. You need to use the global $post in your function.

Exemple of usage:

function my_function_shortcode()
{
    global $post; // important !

    $args = array(
        'post_type' => 'event',
        'post_status' => 'publish',
    );

    $event_query = new WP_Query($args);
    if ($event_query->have_posts()) : while ($event_query->have_posts()) : $event_query->the_post();
            $event_location = get_post_meta($post->ID, 'event_location', true);
        endwhile;
    endif;
    wp_reset_query();
}

add_shortcode('my_function_output', 'my_function_shortcode');
Mz1907
  • 625
  • 4
  • 10