I’m making a website and want to display Events. Therefore I made a Custom Post Type called ‘event’.

I’m querying events to show them in a page. The Events I made also have some custom fields like : ‘Location’, ‘Day’, ‘Month’, ‘Year’ and a category.

This is my code now :

    <?php
        $args = array(
          'post_type' => 'event', 
        );

        $events = new WP_Query( $args );

        if( $events->have_posts() ) {
          while( $events->have_posts() ) {
            $events->the_post();

            ?>
              <div class="event">
                  <div class="event-date">

                  </div>
                  <div class="event-content">
                        <div class="event-title"><?php the_title() ?></div>
                        <div class="event-info">
                            <span>Location: </span>
                            <span>Category: </span>
                        </div>
                  </div>
              </div>
            <?php
          }
        } else {
          echo 'No events!';
        }
      ?>

Which only gives me the title. How can I display the custom fields and the category ?

I am fairly new to WordPress development, used to programming in .NET.

Thx for any help!

1 Answer
1

Custom fields are saved in the post_meta table. In your query you got the post title and post ID, so now you have to get post meta.

Use:

<?php
get_post_meta( get_the_ID(), '_location', true );
?>

same for the rest of your custom fields only ‘_location’ will change according to field you are getting.
Read more about post meta here

Leave a Reply

Your email address will not be published. Required fields are marked *