wppaste
WordPress

stick_post( int $post_id )

Since
2.7.0
Source
wp-includes/post.php:3239
Makes a post sticky.

Description

Sticky posts should be displayed at the top of the front page.

Compatibility

WordPress
since 2.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$post_idint
Post ID.

Performance profile

How much work a call to stick_post() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
17–37

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 39.

Plugin surface
1 hook

Third-party callbacks on 'post_stuck' run inside this call, and their cost is not bounded by anything here.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksdo_action()called directly
  • cacheobject cachewp_cache_get()one call below stick_post()
  • serializeserialisationmaybe_unserialize()one call below stick_post()

Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost stick_post() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_array($stickies) && $post_id17get_option()
!is_array($stickies) && $post_id21get_option(), do_action()
is_array($stickies) && $post_id23get_option(), array_map(), array_unique()
!is_array($stickies) && !$post_id27get_option(), array_values(), update_option()
is_array($stickies) && $post_id27get_option(), array_map(), array_unique(), do_action()
!is_array($stickies) && !$post_id31get_option(), array_values(), update_option(), do_action()
is_array($stickies) && !$post_id33get_option(), array_map(), array_unique(), array_values(), update_option()
is_array($stickies) && !$post_id37get_option(), array_map(), array_unique(), array_values(), update_option(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev3917–373
8.53917–373
8.43917–3733 fewer instructions than PHP 8.3
8.34220–403
8.24220–403
8.14220–403
7.44220–403

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while stick_post() runs, in this order:

  1. do_action( post_stuck )actionline 3263 (+24 into the body)

    Fires once a post has been added to the sticky list.

Uses · 3

  • get_option()Retrieves an option value based on an option name.
  • update_option()Updates the value of an option that was already added.
  • do_action()Calls the callback functions that have been added to an action hook.

Used by · 5

Source code

function stick_post( $post_id ) {	$post_id  = (int) $post_id;	$stickies = get_option( 'sticky_posts' );	$updated  = false; 	if ( ! is_array( $stickies ) ) {		$stickies = array();	} else {		$stickies = array_unique( array_map( 'intval', $stickies ) );	} 	if ( ! in_array( $post_id, $stickies, true ) ) {		$stickies[] = $post_id;		$updated    = update_option( 'sticky_posts', array_values( $stickies ) );	} 	if ( $updated ) {		/**		 * Fires once a post has been added to the sticky list.		 *		 * @since 4.6.0		 *		 * @param int $post_id ID of the post that was stuck.		 */		do_action( 'post_stuck', $post_id );	}}

Changelog

Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.