Set a custom image field as the default social share image for post type

#700723
  • Resolved Anonymous
    Rank Math free

    Hi team,

    Is it possible to set one of my image fields as the default social share image? We have a specific image for our case study post type that we would like to use for the social share image and would rather not set every single case study up.

    Cheers,
    Matt

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello,

    I apologize for the inconvenience you’re facing. Yes, it is possible to set a custom image field as the default social share image for your case study post type. You can achieve this by using Rank Math’s filter to override the first image in the post and make the default Open Graph thumbnail the Open Graph/Social Preview image.

    Here’s the filter you can use:

    add_filter('rank_math/opengraph/pre_set_content_image', function() {
        return true;
    });

    By adding this filter, you can ensure that the default Open Graph thumbnail will be used as the social share image for your case study post type.

    If you have any further questions or need assistance with this, please feel free to let us know.

    Best regards.

    Sources:
    Open Graph Meta Tags » Rank Math
    How to Use the Watermark Option for Social Sharing in Rank Math PRO » Rank Math
    Social Media Options in Rank Math
    Rank Math ‘Titles and Meta’ Options and Settings

    Hello,

    Let us know if you have already applied the filter successfully to force the use of the default Opengraph image for your posts.

    If you’re not sure how to add this code, you can follow this guide:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Looking forward to helping you.

    Anonymous
    Rank Math free

    Hi Reinelle,

    This is not what I require, sorry. I want to use a custom image field as the default social image on a specific custom post type, rather than have to go and add a social image to each one.

    I am not really sure what this code has done on the site.

    Cheers,
    Matt

    Hello,

    If you are using ACF to add the custom fields, you can use the following filter:

    add_filter( "rank_math/opengraph/facebook/image", function( $attachment_url ) {
    	if(get_post_type() == 'post type name') { //name of the post type
    		$attachment_url = get_field('field-name', get_queried_object_id()); //name of the custom field
    		return $attachment_url;
    	}
    	return $attachment_url;
    });

    Hope that helps, and please do not hesitate to let us know if you need our assistance with anything else.

    Anonymous
    Rank Math free

    Thanks,

    I actually created them with JetEngine which is somewhat similar. I have added the below code to auto add the tag for this post type. Thanks for the assistance.

    add_action('wp_head','banner_image_opengraph');
    function banner_image_opengraph() {
    	if(get_post_type() == 'case_study') { //name of the post type
    		$attachment_url = get_post_meta(get_the_ID(),'banner-image', true); //name of the custom field
    		echo "<meta property='og:image' content=$attachment_url>";
    	}
    }

    Cheers,
    Matt

    Hello,

    We are super happy that you are able to sort this out.

    If you have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

Viewing 6 replies - 1 through 6 (of 6 total)

You must be logged in to reply to this ticket.