I created posts with custom post types, and it’s been working OK.
Today I added __( ‘string’, ‘text-domain’ ) to my codes, and those custom posts are now not query-able any more.

I create custom post type Item.

/*** ITEM ***/
$args = array(
    'labels' => array(
        'name' => __( 'Items' ),
        'singular_name' => __( 'Item' ),
        'add_new' => __( 'Add Item', 'Item' ),
        'add_new_item' => __( 'Add New Item' ),
        'edit_item' => __( 'Edit Item' ),
        'new_item' => __( 'New Item' ),
        'view_item' => __( 'View Item' ),
        'search_items' => __( 'Search Item' ),
        'not_found' => __( 'No Item Found' ),
        'not_found_in_trash' => __( 'No Item In Trash' ),
        'parent_item_colon' => ''
    ),
    'menu_icon' => 'dashicons-cart',
    'public' => true,
    'publicly_queryable' => true,
    'show-ui' => true,
    'query_var' => true,
    'menu_position'  => 100,
    'has_archieve' => true,
    'supports' => array( 'title', 'revisions' ),
    'rewrite' => array( 'slug' => 'item', 'with_front' => false ),
    'taxnomies' => array( 'large_feature', 'small_feature' ),
    'capability_type' => 'post',
    'hierarchical' => true,
    'capabilities' => array(
    'publish_posts' => 'publish_items',
    'edit_posts' => 'edit_items',
    'edit_others_posts' => 'edit_items',
    'delete_posts' => 'delete_items',
    'delete_others_posts' => 'delete_items',
    'read_private_posts' => 'read_private_items',
    'edit_post' => 'edit_items',
    'delete_post' => 'delete_items',
    'read_post' => 'read_items',
    'manage_categories' => 'manage_item_categories',
    )
);
register_post_type( 'item', $args );

Then I create posts with the post type of Item.

$target_item[0] = array( 'slug' => 'product', 'title' => __( 'Product', 'book-keeper' )  );
$target_item[1] = array( 'slug' => 'service', 'title' => __( 'Service', 'book-keeper' ) );
$target_item[2] = array( 'slug' => 'uncategorized-income', 'title' => __( 'Uncategorized Income', 'book-keeper' ) );

$cnt_target_items = sizeof( $target_item );

for( $i = 0; $i < $cnt_target_items; $i++ ) {

    $item_id = wp_insert_post( array(
        'post_title'=> $target_item[$i]['title'],
        'post_type' => 'item',
        'post_name' => $target_item[$i]['slug'],
        'post_status' => 'publish',
        'comment_status'=> 'closed',
    ));

} // end for

Now I can see the posts ( product, service, uncategorized-income ) in my Admin panel. I can also see them in my {table-name}_posts table. Their post type is properly set as Item.

I just checked the {table-name}_posts table and found that their post IDs are as below.

  • product: 4115
  • service: 4116
  • uncategorized-income: 4117

I can echo the title of my custom posts using their post ID like below

echo get_the_title( 4115 );

, which correctly echoes ‘product’.

However, when I query them like below, the return is empty.

$items = get_posts( array( 
    'post_status' => 'publish',
    'post_type' => 'item',
    'posts_per_page' => -1 
) );
print_r( '<pre>'.$items.'</pre>' );

The thing is, I didn’t have problem with this query before I added this __( 'string' ) to my code, but I cannot find what I did wrong with it.

What did I wrong here?

Read more here: Posts created using __( ‘string’, ‘text-domain’ ) not queriable


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: