How to retrieve sticky post in raw sql?

How I could made a sql query who retrieve all the sticky post in a wordpress database? This is because we use a new app over the old wordpress website database, and I don’t know where WordPress “store” the sticky attribute.

Thanks in advance

Related posts

Leave a Reply

2 comments

  1. It’s not stored as attribute, but as list of all stickies. Snippet from core:

    $sticky_posts = get_option('sticky_posts');
    

    So look for the option and then use IDs to retrieve posts.

  2. In raw SQL it would look like this:

    SELECT option_value FROM `wp_options` where option_name = 'sticky_posts'
    

    It will return string similar to:

    a:2:{i:0;i:27935;i:1;i:33322;}
    

    Which means that posts with IDs: 27935 and 33322 are “sticky”.
    If there are no sticky posts the returned string will look as follows:

    a:0:{}
    

    For more details about WordPress serialized data structure view https://codex.wordpress.org/Function_Reference/maybe_serialize

    Anyway if you are developing custom theme or plugin it’s much better to use snippet provided by @Rarst than writing custom queries.