I'm trying to output different text for specific posts within a custom post type within a loop. This is the loop:

    'post_type'     => 'products',
    'post_status'   => 'publish',
    'post_parent'   => 0,
    'order'         => 'ASC',
    'orderby'       => 'menu_order'
$query_parent_products = new WP_Query($args);

So I'm already within the custom post type category, I now need to target a specific ID of a custom post with a conditional statement such as:

if ($post->ID == 116) {
    _e ('my ID is 116', 'theme_domain');
} else {

I've tried several different attempts but can't get it to work.

UPDATE: This is how I've got my cpt set up:

add_action( 'init', 'products_post_type');
function products_post_type() {
    $labels = array(
        'name'                  => 'Products',                              // This is the Title of the Group
        'singular_name'         => 'Product',                               // This is the individual type
        'add_new'               => 'Add New',                                   // The add new menu item
        'add_new_item'          => 'Add New Product',                       // Add New Display Title
        'edit'                  => 'Edit',                                      // Edit Dialog
        'edit_item'             => 'Edit Products',                             // Edit Display Title
        'new_item'              => 'New Product',                           // New Display Title
        'view_item'             => 'View Product',                          // View Display Title
        'search_items'          => 'Search Products',                       // Search Custom Type Title
        'not_found'             => 'No products yet, why not create some?',     // This displays if there are no entries yet
        'not_found_in_trash'    => 'No products found in Trash',                // This displays if there is nothing in the trash
        'parent_item_colon'     => ''
    register_post_type( 'products', array(
        'labels'                => $labels,
        'public'                => true,
        'publicly_queryable'    => true,
        'exclude_from_search'   => false,
        'show_ui'               => true,
        'menu_position'         => 20,                                          // this is what order you want it to appear in on the left hand side menu
        'menu_icon'             => 'dashicons-index-card',                      // the icon for the custom post type menu. uses built-in dashicons (CSS class name)
        'query_var'             => true,
        'rewrite'               => array(
            'with_front' => false,
            'slug' => 'products'                                                 // you can specify its url slug
        'capability_type'       => 'page',
        'has_archive'           => true,
        'hierarchical'          => true,
        'taxonomies'            => array('post_tag'),
        'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'revisions', 'page-attributes')


I assume you're looping through your query the usual way, if so, use the following code

if ( $query_parent_products->have_posts() ) :
    while ( $query_parent_products->have_posts() ) {

        // your post specific logic here
        if ( get_the_ID() == 116 ) {
            _e ('my ID is 116', 'theme_domain');
        } else {
  • Thanks. My loop is set up as exactly you have it but unfortunately this only outputs the title for all posts and doesn't change for the specific ID. Could this be something to do with the way my cpt are set up? I've updated my question with that code. – Mr Jonny Wood Jul 3 '17 at 8:44
  • There's nothing wrong with your CPT register code. Maybe you're targeting incorrect post ID. Also, please debug by echoing value of get_the_ID() and see if it works. – Junaid Jul 3 '17 at 8:51
  • I was targetting the wrong id!!!! This is what happens on 4hrs sleep because you're up all night coding! Thanks for you help!! – Mr Jonny Wood Jul 3 '17 at 8:52
  • Take a break champ. When you can't tackle a bug, stop working, go for a walk or have coffee. Relax and then start working on the bug when you're fresh. – Junaid Jul 3 '17 at 8:53

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.