posts status in wordpress

How to add custom statuses for posts

Post status indicates information about the status of a written and published article. By default, WordPress has the following statuses available:

  • Public (visible to everyone)
  • Private (visible only to certain people)
  • Password protected (visible to anyone entering a specific password when accessing the post)

To create a new status that will be displayed as a new selection option along with the 3 above, you will need to add the following code to the theme’s functions.php file:

function status_stare_postare_noua() {

	$args = array(
		'label' => _x( 'numele-starii', 'Status General Name', 'text_domain' ),
		'label_count' => _n_noop( 'numele-starii (%s)',  'numele-starilor (%s)', 'text_domain' ), 
		'public'=> true,
		'show_in_admin_all_list' => true,
		'show_in_admin_status_list' => true,
		'exclude_from_search'  => false,
	);
	register_post_status( 'starea-noua', $args );

}
add_action( 'init', 'status_stare_postare_noua', 0 );

Brief explanation of the code:

  1. A function is created that will generate information about the newly created state (we named it new_posting_state_status (), but it can have any name you want)
  2. In the created function the following are introduced:
    1. The $ args array that will contain:
      • status name
      • the singular and plural of the noun
      • whether the post will be publicly visible or not (true / false)
      • if it will be visible to logged in users who publish / edit articles
      • status can be displayed or excluded from searches
    2. Registering the new status using the default WordPress function register_post_status ().
  3. After closing the function, a WordPress action hook is invoked, respectively add_action () which will contain at least two arguments: the first will say what type of action is invoked (in our case “init”) and the second will specify the function in which created the new status.
Share: