0

I'm using this plugin (FantasticElasticSearch) to index content when it's published. So far it was strightforward and an easy task but, now that I tried to index a flag saying if a post is sticky, I'm having trouble and it's being impossible.

I'm changing the addOrUpdate function of the plugin to my profit, I'm adding some data (featured image, featured video...) and as I said I want to index if a post is sticky. The problem is that the database is not updated in the index moment, and I don't know how to hook to that event.

This is what's happening now:

  1. I publish a post, non-sticky.
  2. I check if the post is sticky to add that information to the index, so far all is good and it's indexed well (as a non-sticky post is_sticky returns false).
  3. I edit the post and turn it to an Sticky post. When I check if the post is sticky it returns false again, as if it wasnt updated in the database yet.

So, where do I have to hook? Can I force the update of the database with the sticky/nonsticky information with some function?

The function (addOrUpdate) that I'm rewritting is the one that's in this file.

I turned it into something like this:

static function addOrUpdate($post){
    $type = self::_index(true)->getType($post->post_type);
    $data = self::_build_document($post);

    //filling $data with extra fields I'd like to index
    $data['is_sticky'] = is_sticky();

    //now the document is added to elastic.
    $type->addDocument(new \Elastica\Document($post->ID, $data));       
}

The addOrUpdate function hooks into the save_post event as you can see in the hooks.php file.

I'm still unable to get this working, I'd aprecciate any help you could give me on this matter. By the way, I already tried Pat J's method (is_sticky with Id post as a parameter, but I'm in the loop as I'm in the save_post action).

Finally solved it by looking in the $_POST array. $_POST['sticky'] is only informed when a POST is (or is being made) sticky.

4
  • Where is your code? Where/how are you checking for "is_sticky"?
    – s_ha_dum
    Commented Jun 5, 2015 at 14:30
  • I'm rewritting the addOrUpdate function that there's in this file: [github.com/parisholley/wordpress-fantastic-elasticsearch/blob/…. I'll edit the post with more info.
    – Astaroth
    Commented Jun 5, 2015 at 14:54
  • Is this code run once, or does it run every time a post is updated?
    – Pat J
    Commented Jun 5, 2015 at 16:38
  • Everytime a post is published/updated. It hooks into the save_post action.
    – Astaroth
    Commented Jun 6, 2015 at 0:54

2 Answers 2

1

As I said in the question post, I finally solved it by looking into the $_POST global. There's a $_POST['sticky'] field (valued as 'sticky') that is there just when a Post is sticky or is being made sticky, and it's not when a post is not sticky or is being unsticked.

0

If you're not in The Loop, you should pass an ID to is_sticky():

static function addOrUpdate($post){
    $type = self::_index(true)->getType($post->post_type);
    $data = self::_build_document($post);

    //filling $data with extra fields I'd like to index
    $data['is_sticky'] = is_sticky( $post->ID );

    //now the document is added to elastic.
    $type->addDocument(new \Elastica\Document($post->ID, $data));       
}

This assumes that the $post parameter you pass in is a WP_Post object.

Reference

1
  • Yes, I should have said this but I've already tried. I even tried getting the sticky post ID's (get_option( 'sticky_posts' )) but the post was between the Ids when I tried to remove it and it wasn't when I tried to stick it.
    – Astaroth
    Commented Jun 6, 2015 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.