Support

Account

Home Forums Backend Issues (wp-admin) Change post featured image based on user status

Solved

Change post featured image based on user status

  • Hi guys,
    im quite new here, just found out how many things I can do with ACF and it’s amazing!

    I’ve created a field group called ‘main_image’ and under it I have 2 fields
    1.
    ‘main_image_logged’
    type: image

    2.
    ‘main_image_logout’
    type:image

    what im trying to do is to show the classic featured image which comes with the post for all, and for users that is logged in show the image in field “main_image_logged’

    for the logout i even tried to set name:_thumbnail_id which took the image from ‘main_image_logout’ and used it as a featured image.

    is there any way how to do this?
    if user is logged out -> featured image from field ‘main_image_logout’
    if user is logged in -> featured image from field ‘main_image_logged’

    thanks alot guys

  • No. The WP featured image can only have one image assigned to it.

    This is a WP question, not really an ACF question. You would need to filter WP functions that get and show the post thumbnail. For example this function that gets the thumbnail ID is filterable https://developer.wordpress.org/reference/functions/get_post_thumbnail_id/.

    I could potentially give you code to alter this but I have no idea what else would need to be modified.

  • Thank you John,

    any idea of a possible workaround how to get it done with ACF ? without using the native WP thumbnails ?
    in any case thank you for your time

  • I tried something like this

    function acf_set_featured_image( $value, $post_id, $field  ){
    if (is_user_logged_in()) {    
        if($value != ''){
    	    //Add the value which is the image ID to the _thumbnail_id meta data for the current post
    	    add_post_meta($post_id, '_thumbnail_id', $value);
        }
     
        return $value;
    }
    }
    add_filter('acf/update_value/name=main_image_logged', 'acf_set_featured_image', 10, 3);

    but its wrong

  • As I said, a post can only have one “_thumbnail_id”. I did some looking and it appears that all thumbnail function eventually call get_post_thumbnail_id() and like I said, the value returned can be filtered.

    
    add_filter('post_thumbnail_id', 'replace_thumbnail_id_with_acf', 20, 2);
    function replace_thumbnail_id_with_acf($thumbnail_id, $post) {
      $user_id_associated_with_post = /*you need to figure this out*/;
      // I don't have the condition here.
      // figuring out if some user on the site is logged in
      // when it is not the current user
      // is a complicated process
      // this is not something that WP will provide
      if (/*is the user associated with this post logged in?*/) {
        $image_id = get_field('image_when_logged_id', $post->ID, false);
        if ($image_id) {
          $thumbnail_id = $image_id;
        }
      } else {
        $image_id = get_field('image_when_logged_out', $post->ID, false);
        if ($image_id) {
          $thumbnail_id = $image_id;
        }
      }
      return $thumbnail_id;
    }
    

    As far as figuring out if the user that is associated with the given post is logged in or logged out, I found this: https://wordpress.stackexchange.com/questions/34429/how-to-check-if-a-user-not-current-user-is-logged-in

  • @hube2 thank alot !

    small question, when my field name is _thumbnail_id
    can i still get it by using get_field(‘_thumbnail_id’,..) ?

  • _thumbnail_id can only have one value, you need 2 fields for 2 images or you need _thumbnail_id (which is the default meta_key for the WP featured image) for one and another field name for the second image.

  • John, Im not quite sure if I understand
    I do have 2 fields
    1. with name “_thumbnail_id” with which I can set the default featured image of a post.
    – which i found as a comment on this forum provided by you – thanks again
    2. with name “main_image_logged” where the image which can be seen only by logged in users.

  • @hube2 John,

    your solution gave me right direction !!

    for anyone looking for the same or similiar solution here is the code

    add_filter('post_thumbnail_id', 'replace_thumbnail_id_with_acf', 20, 2);
    function replace_thumbnail_id_with_acf($thumbnail_id, $post) {
    
    if ( is_user_logged_in() ) {
        $image_id = get_field('reveal_face', $post->ID, false);
        if ($image_id) {
          $thumbnail_id = $image_id;
        }
    } else {
        $image_id = get_field('_thumbnail_id', $post->ID, false);
        if ($image_id) {
          $thumbnail_id = $image_id;
        }
    }
    return $thumbnail_id;
    }
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.