0

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?

2

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

    foreach ( $attachments as $post ) {  
setup_postdata($post);
                the_title();  
        the_content();  
    }
  • Thanks again! I also need to put $post; right at the start of the second function. I just need to find out why now (very much a newbie). – mattnewbie May 9 '13 at 23:04
1

My guess is that you'll need to use setup_postdata($post); inside of your foreach loop. https://codex.wordpress.org/Function_Reference/setup_postdata

  • @mattnewbie - Then why have you marked the other answer with the same solution as the answer if it is not correct? :/ – t31os May 10 '13 at 9:13
1

Both the_title and the_content are template tags and the operate inside the Loop based on the global variable $post which is setup by setup_postdata or by the_post (which runs setup_postdata). What is happening is that those tags are using whatever values were last shoved into the global $post variable. You can run setup_postdata in your function as already suggested or do something like:

foreach ( $attachments as $post ) {  
    echo apply_filters('the_title',$post->post_title));  
    echo apply_filters('the_content',$post->post_content));  
}  
0

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.

  • Can you please explain this answer. – s_ha_dum May 9 '13 at 23:05
  • I'd love to be able to explain the answer, but I don't understand it myself. I visited the setup_postdata($post); codex page someone linked to & just copied it. Here is the codex page codex.wordpress.org/Function_Reference/setup_postdata I will post the full working code above! Once , I've figured out why that works I'll get back to you. – mattnewbie May 9 '13 at 23:09
  • Please familiarize yourself with the WYSIWYG post editor-- especially the button that looks like {}. – s_ha_dum May 9 '13 at 23:20

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.