Question: I have got a shortcode that displays a custom post type, via a `foreach`. The shortcode has been used in the standard post type. I have used `the_title();` in the shortcode. `the_title();` outputs the post\_title of the standard post type (which the shortcode is in), rather than `the_title();` of the custom post type. My code: 

```
function faq_register_shortcode()  {  
  add_shortcode( 'display_faqs', 'mp_faq_shortcode' );  
}  
function mp_faq_shortcode() {    
  $args = array( 'post_type' => 'faq_type_mp', 'numberposts' => 3, 'post_status' => 'any', 'post_parent' => null );   
  $attachments = get_posts( $args );  
  if ($attachments) {   
    foreach ( $attachments as $post ) {  
                the_title();  
        the_content();  
    }  
  } 
}

```

So for example, if the standard post type (which the shortcode is in), had a `post_title` of 'this is the standard post type', this is what is outputted in the shortcode. I want to display the `post_title` which belongs to the custom post type, NOT the standard post (which the shortcode is in). 

What have I done wrong please?

Answer: 
You need to add `setup_postdata($post);` inside your loop:

```
    foreach ( $attachments as $post ) {  
setup_postdata($post);
                the_title();  
        the_content();  
    }

```
**THE ANSWER**

This is the working code 

```
function faq_register_shortcode()  {  
  /* register the [display_faqs] shortcode */  
  add_shortcode( 'display_faqs', 'mp_faq_shortcode' );  
}    
function mp_faq_shortcode() {   
  global $post;  
  $args = array( 'post_type' => 'faq_type_mp', 'numberposts' => 3, 'post_status' => 'any', 'post_parent' => null );   
  $attachments = get_posts( $args );    
  if ($attachments) {  
    foreach ( $attachments as $post ) {   
      setup_postdata($post);  
      the_title();  
      the_content();  
    }    
  }   
}  

```

Based on this codex page (someone linked to) <https://codex.wordpress.org/Function_Reference/setup_postdata> . Sorry I can't explain it further, because I don't currently understand why it works.