#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Custom Post Types & ACF

Structuring Content Beyond Posts and Pages

Custom Post Types let you create entirely new content structures, such as Products, Testimonials, or Portfolio Items, separate from default Posts and Pages.

Creating Custom Post Types

Register a custom post type using code in functions.php, or use a plugin like Custom Post Type UI for a no-code setup.

Each custom post type gets its own admin menu, making content management cleaner for large, structured sites.

Extending Fields with ACF

Advanced Custom Fields (ACF) lets you add custom input fields, such as price, rating, or location, to any post type.

This combination of custom post types and ACF fields is commonly used to build directories, portfolios, and real estate or job listings.

ConceptWhat It Does
Custom Post TypeDefines a new content structure (e.g. Products)
ACF FieldAdds a specific data input (e.g. Price, Rating)
Combined usePowers directories, portfolios, listings

Custom content structures often need to reach a wider audience, which is where multilingual setup comes in.

Registering a Post Type in Code

Registering a post type yourself gives you full control and no plugin dependency. Place the code in a small custom plugin or a child theme; a plugin is safer, because switching themes would otherwise make the content disappear from the admin.

<?php
add_action( 'init', function () {
    register_post_type( 'property', array(
        'labels' => array(
            'name'          => 'Properties',
            'singular_name' => 'Property',
            'add_new_item'  => 'Add New Property',
        ),
        'public'       => true,
        'has_archive'  => true,
        'rewrite'      => array( 'slug' => 'properties' ),
        'menu_icon'    => 'dashicons-building',
        'supports'     => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'show_in_rest' => true,   // enables the block editor and REST API
    ) );

    register_taxonomy( 'property_city', 'property', array(
        'label'        => 'Cities',
        'hierarchical' => true,
        'show_in_rest' => true,
    ) );
} );

After adding a new post type or changing its slug, visit Settings > Permalinks and click Save. This flushes rewrite rules; skipping it is the usual reason new single pages return a 404 error.

Worked Example: A Property Listing With ACF Fields

With the post type registered, create a field group in ACF named "Property Details" and add fields such as price (Number), bedrooms (Number), location (Text) and brochure (File). In the group's location rules, choose "Post Type is equal to Property" so the fields appear only on that screen.

Next, create a template file in your theme named single-property.php. WordPress picks it automatically for single property pages, and archive-property.php for the listing page.

<?php get_header(); ?>

<?php while ( have_posts() ) : the_post(); ?>
  <h1><?php the_title(); ?></h1>

  <?php
  $price    = get_field( 'price' );
  $bedrooms = get_field( 'bedrooms' );
  $location = get_field( 'location' );
  ?>

  <ul>
    <?php if ( $price ) : ?>
      <li>Price: <?php echo esc_html( number_format( $price ) ); ?></li>
    <?php endif; ?>
    <li>Bedrooms: <?php echo esc_html( $bedrooms ); ?></li>
    <li>Location: <?php echo esc_html( $location ); ?></li>
  </ul>

  <?php the_content(); ?>
<?php endwhile; ?>

<?php get_footer(); ?>

get_field() returns the value so you can check and format it, while the_field() prints it directly. Escaping with esc_html() protects the page if an editor ever enters unexpected characters.

Querying Custom Content by Field Values

To list, say, homes under a certain price, use WP_Query with a meta query on the ACF field:

$homes = new WP_Query( array(
    'post_type'      => 'property',
    'posts_per_page' => 10,
    'meta_query'     => array(
        array(
            'key'     => 'price',
            'value'   => 5000000,
            'compare' => '<=',
            'type'    => 'NUMERIC',
        ),
    ),
) );

Always call wp_reset_postdata() after looping through a custom query so the main page data is restored.

Mistakes to Avoid With CPTs and Custom Fields

  • Using a generic post type name like item or post_type. Choose a specific, prefixed name to avoid clashes, and keep it to 20 characters or fewer.
  • Forgetting show_in_rest, which leaves the post type in the classic editor and hidden from the REST API.
  • Using taxonomies as fields or fields as taxonomies. Use a taxonomy when you need to group and filter (city, type); use a field for unique details (price, area).
  • Heavy meta queries on large sites. Filtering many posts by custom fields can be slow; cache results or use taxonomies for common filters.
  • Not exporting field groups. Use ACF's JSON sync or export so fields are version-controlled and portable between sites.

Ready to master WordPress Training Course?

Join Uncodemy's hands-on, mentor-led WordPress training and build real, live websites.

Explore Course