I am currently using a custom query to grab related posts. I am new to php and I am having an issue to display related posts

  php, wordpress

I was wondering how can I grab the posts from the same category from WordPress? I created a post-type ‘job’. Also, I am not sure how to not grab the current post I am currently on. I understand this code is a mess, but anyone who can correct me will be wonderful.

I put this code on my single.php.


$orig_post = $post;
global $post;

$categories = wp_get_post_tags($post->ID);
if ( $categories ) {
    $category_ids = array();
    foreach($categories as $individual_category) 
        $category_ids[] = $individual_category->term_id;

    $args = array(
        'category__in'        => wp_get_post_categories($post->ID),
        'post__not_in'        => array ( get_the_ID() ),
        'posts_per_page'      => 3, // Number of related posts that will be shown.
        'ignore_sticky_posts' => 1,
        'post_type'           => 'job',
        'wp_tax_query'        => array(
            'taxonomy'        => 'category',
            'field'           => 'slug',
    $card_number = 0;
    $my_query = new wp_query( $args );
    if( $my_query->have_posts() ) {
        while( $my_query->have_posts() ) {

    <div class="cards card-<?php echo ++$card_number; ?>">
      <!-- card header -->
      <div class="card-head">
        <a href="<?php the_permalink(); ?>">
            <h2 class="company-name"><?php the_field( 'job_title' ); ?></h2>
        <i class="far fa-heart like"></i>
      <h4 class="job-title"><?php the_field( 'company_name' ); ?></h4>
      <!-- card middle layer: info about company -->
      <div class="card-info">
        <div class="locate-info">
          <i class="fas fa-map-marker-alt info-icons"></i>
          <h5 class="job-locate"><?php the_field( 'location' ); ?></h5>
        <div class="types-info">
          <i class="fas fa-briefcase info-icons"></i>
        <h5 class="job-types"><?php the_field( 'type' ); ?></h5>
      <!-- bottom layer: posted date -->
      <div class="card-bottom-layer">
        <p class="dates"><?php the_field( 'date' ); ?></p>
        <i class="fas fa-info-circle"></i>

<?php the_time('M j, Y') ?>
$post = $orig_post;

Source: Ask PHP