Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

get_template_part() – post-meta not working?

Try globalizing $post inside of event-item.php.

Also: be sure to call wp_reset_postdata() after you close your $loop while loop.

e.g.:

<!-- event-item.php -->
<?php 
// globalize $post
global $post; 
?>
<div id="event-<?php the_ID(); ?>" <?php post_class(); ?>>

    <div class="event-date"><?php echo $event_date; ?></div>
    <div class="event_time"><?php echo $event_time; ?></div>
    <div class="event-speaker"><?php echo $event_speaker; ?></div>
    <div class="event-title"><a href="https://wordpress.stackexchange.com/questions/49285/<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
    <div class="event-description-excerpt"><?php the_excerpt(); ?></div>

</div>
<!-- event-item.php -->

and then:

<?php 
endwhile; 

// Restore $post global to the primary query
wp_reset_postdata();
?>

Edit

Assuming the problem is just the post meta data, I would suggest moving the post meta data variables inside loop-item.php. Only a guess, but perhaps your local variables aren’t getting passed through the include() function that is part of get_template_part().

So, like this:

<!-- event-item.php -->
<?php
// Define these here, inside loop-item.php
$wr_event_fields = get_post_custom();
$event_date_timestamp = $wr_event_fields['_wr_event_date'][0];
$event_date = strftime('%d.%m.%Y', $event_date_timestamp);
$event_time = $wr_event_fields['_wr_event_time'][0];
$event_speaker = $wr_event_fields['_wr_event_speaker'][0]; 
?> 
<div id="event-<?php the_ID(); ?>" <?php post_class(); ?>>

    <div class="event-date"><?php echo $event_date; ?></div>
    <div class="event_time"><?php echo $event_time; ?></div>
    <div class="event-speaker"><?php echo $event_speaker; ?></div>
    <div class="event-title"><a href="https://wordpress.stackexchange.com/questions/49285/<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
    <div class="event-description-excerpt"><?php the_excerpt(); ?></div>

</div>
<!-- event-item.php -->

Also, to avoid undefined variable notices, you should define your variables using isset() conditionals; e.g. change this:

$event_date_timestamp = $wr_event_fields['_wr_event_date'][0];

…to this:

$event_date_timestamp = ( isset( $wr_event_fields['_wr_event_date'][0] ) ? $wr_event_fields['_wr_event_date'][0] : false );

Related Posts:

  1. Grossly inefficient wordpress loops!
  2. WP_Query on custom post type not displaying, multiple loops & get_template_part
  3. Displaying custom posts only if custom meta box’s date is not expired
  4. Create a random unique 6 digit number as custom field for custom post type
  5. get_template_part for each level of taxonomy term
  6. get_template_part and template file names
  7. Meta Key Value in current-user-only loop
  8. Tag page only display 10 posts
  9. Create Array from custom post type to display a slider
  10. How to retrive Custom Post Type Meta Fields in Custom WP_Query
  11. On click some element i want to use some template
  12. Custom meta fields not showing up in WP_Response Object via custom endpoint
  13. get_post_meta for Custom Post Type ( CPT )
  14. How to create content automatically when a post is published?
  15. why get_post_meta is returning 0?
  16. Update postmeta Parent when post_status child change
  17. WP Query with categories only shows one post and ignores the category
  18. What to and how to proceed with CPT to make DB small and efficient?
  19. Metabox not show in categories custom post type cmb2
  20. WordPress request fiter order by related post’s post_title
  21. Post ID randomly printing on page
  22. A custom post within a custom post
  23. Display Custom Taxonomy Alphabetically
  24. Full Custom Post Type List Organised by two Taxonomies
  25. Displaying Custom Posts on a Page
  26. Shortcode not working with post counter
  27. Problem: wp_query outputs all images on site
  28. Update database from Quick Edit according to Checkbox state of Custom Post Type
  29. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  30. Get custom post type’s fields in a while loop as variables
  31. Storing a many to many post type relationship in post meta and keeping SQL ability for Joins
  32. Custom Post Type within the Loop on Homepage (Page Template)
  33. Dynamic Custom Fields
  34. The loop seems stuck to a single (now deleted) post
  35. Show Custom Post Type meta boxes only on Page Edit
  36. Posts per row on archive page (custom post type)
  37. How do I know the ‘meta_key’ for ordering my custom post type
  38. Custom filter function not working with Custom post type
  39. Custom Theme With Custom Loops
  40. Custom Post Types strange pagination problem
  41. Custom post type conditional in loop
  42. Importing Data from a Non-WordPress database, into WP
  43. Listing all term items alphabetically / sorting loop
  44. Getting thumbnails of a custom posts featured images in a page-templates metabox
  45. Display related custom taxonomy posts in sidebar
  46. List Taxonomies: Don’t list taxonomy if it has no post – depending on custom post-meta?
  47. Remove Post Custom Meta Box
  48. pagination not working for category.php (custom post types in categories)
  49. Can’t publish custom post type – “You are not allowed to edit this post.”
  50. Only show posts with date of today or in the future (i.e don’t show past posts)?
  51. How do I control the output of a custom post type in the loop?
  52. Display custom post type posts first, then default posts
  53. Custom Post Type Category Link
  54. Custom Post type category pages template and loop
  55. Custom fields for custom post type
  56. Three Most Recent Posts, One Per Term
  57. Targeting custom post type
  58. Show future events based on custom field AND order by date
  59. Does WP have a global of $id?
  60. WordPress Loop if/else
  61. get_the_title() is returning results from previous loop
  62. Meta Data for Custom Post Type not saving
  63. Using preg_replace to filter custom textarea
  64. Custom Post Loop pulling all custom posts, not just one
  65. Conditional for a Single Post That Belongs to a Category?
  66. Wrapping x posts in html without leaving empty html
  67. WP Query post__in not returning correct results
  68. Custom Post Type single.php template only shows the latest post
  69. Sort posts in loop by the WooCommerce Membership of the author
  70. Check if post with same meta value exists
  71. Filter date from post meta when date is in string format
  72. Including all post id’s of a custom post type into an array
  73. How to include custom post type posts on a page?
  74. 4 column-loop ordered alphabetically with entries grouped (and labeled) by their first letter
  75. Get only used meta_values
  76. If Post Meta Equals Current Title Conditional Advanced Custom Fields
  77. Is there a way to exclude posts based on meta_values?
  78. Do posts, pages and / or custom post type objects have unique ID numbers or can there be multiple objects with the same IDs?
  79. How can I group posts by months and years?
  80. Pagination Error : Duplicate argument being outputted
  81. get_post_type is always post
  82. Archieve.php not loading for custom post type
  83. Multiple Custom Post Type queries, how to DRY it up
  84. Custon Content within WordPress Loop
  85. Trying to Create a PHP Variable for post_type that can be referenced Site Wide
  86. Bootstrap accordion looping through posts incorrectly
  87. Main query not querying any posts in custom taxonomy template
  88. Loop increase number
  89. Custom post type showing same Post on all Pages
  90. Submitting Custom Post Types with custom fields from Front-end form
  91. get_post_meta is not working inside shortcode
  92. Using wp_query to modify the loop in index.php for a CPT
  93. the_post() is printing titles in page footer
  94. create custom meta box with default value
  95. Custom WP_Query doesn’t display all posts
  96. Get $post->ID from a URL
  97. Displaying a div from an assigned meta_value when on a page
  98. Query & the_content() is showing in the header
  99. Meta data (Tags and Categories) for Custom Posts not showing.
  100. Display different information of a custom post type
Categories custom-post-types Tags custom-post-types, get-template-part, loop, post-meta
How to filter a wordpress core function?
.sub-menu or .children?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress