Support » Plugin: Contextual Related Posts » How to override manual posts field?

  • Resolved rinart73

    (@rinart73)


    Good day and thank you for this great plugin.

    Currently I find manual related post field very not user friendly. I would rather prefer graphical way of selecting posts with a search field.
    So I decided to add post selector via Advanced Custom Fields plugin and to use it to override this plugin manual posts field. The problem is that I can’t find a filter in your plugin that would allow me to do that. Any hints?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Ajay

    (@ajay)

    @rinart73 which is the field you’re using in ACF for this? I’d like to see if I can implement something similar in the plugin.

    https://github.com/WebberZone/contextual-related-posts/blob/v3.2.3/includes/admin/modules/metabox.php#L214

    The plugin has a crp_call_meta_box action to add fields, but you don’t have a way to remove fields. I’m not sure if you can override this directly by adding a second field.

    Thread Starter rinart73

    (@rinart73)

    I’m not trying to add custom field into CRP metabox, because implementing proper post selection from scratch is not that easy. So instead I’m relying on ACF to do that.
    Right now I have ACF field with type “Relational -> Relationship” called related_posts. I enabled taxonomy filter and search for it. I set return format as “Post ID”.
    Here is how it looks: https://i.ibb.co/xLvQqGg/image.png

    There are 2 possible way of integrating this field with your plugin manual posts field.

    1. On post save action take related_posts, retrieve crp_post_meta and add value of my custom field into your plugin meta. I decided to not go with this at the time, mostly because I didn’t want to do permanent changes just in case.

    2. Override your plugin manual posts on frontend.
    Your plugin doesn’t have a filter:
    $manual_related = apply_filters('crp_manual_post_ids', wp_parse_id_list( $this->crp_post_meta['manual_related'] ));
    So I had to override crp_post_meta when it’s retrieved.

    if (!is_admin()) {
      add_filter('get_post_metadata', 'gp_child_crp_manual_posts', 100, 4);
      function gp_child_crp_manual_posts($metadata, $object_id, $meta_key, $single) {
        if (isset($meta_key) && 'crp_post_meta' === $meta_key) {
          $related_posts = get_field('related_posts', $object_id);
          if (!empty($related_posts)) {
            $crp_post_meta = gp_child_raw_crp_post_meta($object_id);
            if (!is_array($crp_post_meta)) {
              $crp_post_meta = array();
            }
            $crp_post_meta['manual_related'] = implode(',', $related_posts);
            return array($crp_post_meta);
          }
        }
        // Return original if the check does not pass
        return $metadata;
      }
      
      function gp_child_raw_crp_post_meta($object_id) {
        remove_filter('get_post_metadata', 'gp_child_crp_manual_posts', 100);
        $crp_post_meta = get_post_meta($object_id, 'crp_post_meta', TRUE);
        add_filter('get_post_metadata', 'gp_child_crp_manual_posts', 100, 4);
        return $crp_post_meta;
      }
    }
    • This reply was modified 1 month, 1 week ago by rinart73.
    • This reply was modified 1 month, 1 week ago by rinart73.
    • This reply was modified 1 month, 1 week ago by rinart73.
    Plugin Author Ajay

    (@ajay)

    Do I understand correctly, that you have a separate ACF field that allows you to manually select the related posts which get saved under related_posts field?

    If so, you might have an easier way to retrieving this via the_posts filter of WP_Query as I’ve done in the plugin: https://github.com/WebberZone/contextual-related-posts/blob/master/includes/class-crp-query.php#L779

    I use that to look for any posts set as manual_posts in crp_post_meta and then add those in to what the plugin finds.

    In the next version, I’m exploring trying to lock this in earlier to avoid a query being run if there are more manual posts than the required set

    Thread Starter rinart73

    (@rinart73)

    Ok got it, ty.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.