/*
Theme Name: Avada Child
Description: Child theme for Avada theme
Author: ThemeFusion
Author URI: https://theme-fusion.com
Template: Avada
Version: 1.0.0
Text Domain:  Avada
*/
function get_random_gallery_post_by_term( string $term ) {
    // Setup args
    $args = array(
        'post_type'      => 'gallery',
        'post_status'    => 'publish',
        'posts_per_page' => 1,
        'orderby'        => 'rand',
        'tax_query'      => array(
            // Query post by custom taxonomy term
            array(
                // update your_gallery_taxonomy_name to match your actual taxonomy name
                // You can find taxonomy name on your browser address line when you're on the taxonomy admin page
                'taxonomy'     => 'your_gallery_taxonomy_name', 
                'field'        => 'slug',
                'terms'        => $term
            )
        ),
    );
    // Execute query
    $query = new WP_Query( $args );
    // Return found post or false, if not found
    return $query->posts ? $query->posts[0] : false;
}

// in some template file
$gallery_post = get_random_gallery_post_by_term( 'my-gallery-category' );
// Do something with the post
if ( $gallery_post ) {
    // Thumbnail, empty string if thumbnail not found
    echo get_the_post_thumbnail( $gallery_post->ID, 'thumbnail' );
    // Content
    echo wp_kses_post( $gallery_post->post_content );
}

