Title: Custom Field (via Shortcode) inside Query Loop block
Last modified: May 13, 2022

---

# Custom Field (via Shortcode) inside Query Loop block

 *  Resolved [oliveran](https://wordpress.org/support/users/oliveran/)
 * (@oliveran)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/custom-field-via-shortcode-inside-query-loop-block/)
 * I use a couple of custom field in my posts.
 * To expose the value in the post, I’ve created a simple shortcode function, for
   example this for a custom field called ‘sector’:
 *     ```
       add_shortcode('cf_sector', 'cf_sc_sector');
       function cf_sc_sector($atts=[], $content=null){
         global $post;
         return get_post_meta( $post_id=$post->ID, $key = 'sector', $single = true );
       }
       ```
   
 * This works perfectly in the post, for example /test/test_post
 * However, it does not work when I add the same short code inside the Post Template
   block within a Query Loop example /test_loop
 * On further investigation it seems that the global variable $post is referencing
   the containing page, not the post inside the loop.
 * Am I doing something wrong ??
 * I’m new to WP and this forum so please send me to right location if this isn’t
   it, thanks.
    -  This topic was modified 4 years, 1 month ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).
      Reason: code format fixed
    -  This topic was modified 4 years, 1 month ago by [Jan Dembowski](https://wordpress.org/support/users/jdembowski/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcustom-field-via-shortcode-inside-query-loop-block%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/custom-field-via-shortcode-inside-query-loop-block/#post-15641555)
 * You’re not doing anything wrong. What are you seing is the expected behaviour.
 * If you want the shortcode to interact with a post in a secondary loop (or any
   other post outside of the standard loop), it’s worth passing that post in as 
   part of the arguments and using that.
 * As an example (not tested, could be very broken)…
 *     ```
       function cf_sc_sector($atts=[], $content=null){
         if (array_key_exists ('postid', $atts) {
           $post = get_post ($atts ['postid']);
         }
         else {
           global $post;
         }
   
         return get_post_meta( $post_id=$post->ID, $key = 'sector', $single = true );
       }
       ```
   
 *  Thread Starter [oliveran](https://wordpress.org/support/users/oliveran/)
 * (@oliveran)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/custom-field-via-shortcode-inside-query-loop-block/#post-15642295)
 * Thanks, I’ll give that a try. At least I know I’m not completely insane.
 *  [syoung68](https://wordpress.org/support/users/syoung68/)
 * (@syoung68)
 * [4 years ago](https://wordpress.org/support/topic/custom-field-via-shortcode-inside-query-loop-block/#post-15675490)
 * Any follow up on this?
 *  [arealdadsdad](https://wordpress.org/support/users/arealdadsdad/)
 * (@arealdadsdad)
 * [3 years, 10 months ago](https://wordpress.org/support/topic/custom-field-via-shortcode-inside-query-loop-block/#post-15920904)
 * Are you adding this into functions.php or somewhere else? I’m having a similar
   issue. Thanks!
 *  Thread Starter [oliveran](https://wordpress.org/support/users/oliveran/)
 * (@oliveran)
 * [3 years, 9 months ago](https://wordpress.org/support/topic/custom-field-via-shortcode-inside-query-loop-block/#post-15957218)
 * Hi arealdadsdad,
 * I tried it in functions.php.
    I never got it to work, but could never expose 
   the ID of the post in the loop. in the end I organised my site differently.
 * Andy

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Custom Field (via Shortcode) inside Query Loop block’ is closed to new
replies.

## Tags

 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 4 participants
 * Last reply from: [oliveran](https://wordpress.org/support/users/oliveran/)
 * Last activity: [3 years, 9 months ago](https://wordpress.org/support/topic/custom-field-via-shortcode-inside-query-loop-block/#post-15957218)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
