Improved the output of the related posts in WordPress
The major thing for me in this code is the repetition forms a large part of it.  You can also use some of the features of PHP to stop having to move data around and comparing individual items.

Using array_column() (this requires PHP 7+ as your input is an array of objects) to fetch all of the values of cat_ID, stops you having to extract the values from the objects and then compare each one individually.  You can then use in_array() to check for each value against all of them in one call rather than the list of || conditions.

Then the code setting $args is the same in each branch of your if, so I've reduced this to each branch sets $cat (as your last branch already does) and then this is used by the same code outside the if...

$cat = get_the_category();

$categories = array_column($cat, "cat_ID");
if ( in_array(10, $categories) ){
    $cat = 10;
} elseif (in_array(12, $categories)){
    $cat = 12;
} else {
    $cat = $cat[0]-&gt;cat_ID;
}
$post = get_the_ID();
$args = array('cat'=&gt;$cat, 'orderby' =&gt; 'date',
    'showposts' =&gt; $related,'post__not_in' =&gt; array($post));