WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm rewriting the question, including exemplary code, I hope it will be easier to understand now.

1. My shortcode:

    function testid_shortcode( ) {
         global $post;
         return $post->ID;   

    add_shortcode('testid', 'testid_shortcode');  

2. The issue:

I'm using the shortcode on page Example1 [ID=1]:


Then I'm including it on different page Example 2 [ID=2]:

$included_page = get_page( $included_id ); 
$content = apply_filters('the_content', $included_page->post_content);

The shortcode returns "1" and "2" on these pages, while I want it to return "1" AND "1", so simply speaking I want it to retrieve the ID (specifically metaboxes) of the "original" page.

Is it possible to fix it somehow? I guess it should be done within the shortcode itself, but have absolutely no idea how. I guess passing a variable with the "original" ID and overwriting the one shortcode gets will do the trick, but how to do that?

share|improve this question
I, at least, can't really understand the question. I think you either need to provide more code, get a bit more specific or rewrite part of the question. What does the actual shortcode look like? Is the shortcode supposed to always return the post_meta from ID = 1 or does it return the post_meta from the page it's on? – mrwweb May 8 '12 at 18:56
@mrwweb, check the updated question :) – Wordpressor May 9 '12 at 16:05
Did you ever find a solution for this, I have the same problem where a shortcode function needs to know on what page it was defined. And that page is included (through get_page()) into another: I can only get the post_id from the "hosting" page. – windyjonas Nov 5 '12 at 14:12
up vote 2 down vote accepted

The global $post object is from the current query, so what you're seeing is the expected behavior.

If you always want the shortcode to return the post_meta from ID = 1, then you should just hardcode that into the shortcode like in @Chris_O's answer where you just save a variable that equals 1 and pass that to get_page().

However, it sounds like you're looking for shortcode attributes. Try this:

// shortcode function
function testid_shortcode( $atts ) {
    // extract the variables, if there is no ID attr, the default is 1
    extract( shortcode_atts( array(
        'id' => 1
    ), $atts ) );
    // set page id to either the id attr which defaults to one.
    $page_id = $id;
    $page_data = get_page( $page_id );
    return // ... return something with your shortcode
// register the shortcode
add_shortcode( 'testid', 'testid_shortcode' );

Then you could use:


To return the post object with ID=1 or

[testid id=2]

to return the post object with ID=2.

share|improve this answer
this is not a bad idea, but I'm going to use this shortcode on different pages, I won't know their IDs and I'm looking for something more back-end related (so I won't have to think about IDs, also I'll never now which page I'm going to include so I'd have to add this attribute everywhere). – Wordpressor May 9 '12 at 17:21
Are you trying to say that you want the first instance of the shortcode to "anchor" the rest (I can't think of a better way to say that). It sounds like maybe you need to mark one page with a metabox or custom field and then query to get other data from that page. Shortcodes are context-aware, but it's much harder (to the point that it's not worth doing) to have pages be aware of shortcodes on them. – mrwweb May 9 '12 at 18:59

get_page requires the page id to passed via a variable.

Your giving it a string which is causing WordPress to use the variable from the global $page->ID at the time the function is called.

Just pass the page id as a variable and it should work fine.

$page_id = 1;
$page_data = get_page( $page_id );
share|improve this answer
I'm getting the right page, but it's being rendered wrongly as it gets ID of the page where I'm including it, not the page where it originally is. I hope the new question will be easier to understand. That's the problem I'm trying to overcome. – Wordpressor May 9 '12 at 16:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.