I have a and inside it I would like to add the contents of the first sticky post.

I went on codex.wordpress.org to find more about wp_query and saw this code that I could use to solve my problem:

$sticky = get_option( 'sticky_posts' );
$args = array(
    'posts_per_page' => 1,
    'post__in'  => $sticky,
    'ignore_sticky_posts' => 1
$query = new WP_Query( $args );
if ( $sticky[0] ) {
    // insert here your stuff...

What I don't know is if this is what I am looking for and if yes, what do I need to add in the if statement in order for this code to display the first sticky post inside my div? Thanks in advance!


You could simply follow the example from the Codex:

$sticky = get_option( 'sticky_posts' );
$query = new WP_Query( 'p=' . $sticky[0] );

The variable $sticky will hold an array of IDs, and you access the first sticky post via $sticky[0].

Then you need the WordPress Loop:

if ( $query->have_posts() ) {
  while ( $query->have_posts() ) {
    echo '<div>';
    echo '</div>';

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.