How to get the post type link from controller when looping through a post type

So I have a function on the FrontPage.php called services that is supposed to get one of my post types and everything works great but the link, for some reason I am not able to return the post type link through the Controller

Here is the function on the Controller, so $link is the variable that is supposed to have the post type link. I am using the get_permalink() function to store the url info there but for some reason it doesn’t work

public function services()
    $args = [
        'post_per_page' => -1,
        'offset' => 0,
        'orderby' => 'date',
        'order' => 'ASC',
        'post_type' => 'services'
    $the_query = new\WP_Query($args);
    $services = [];
    if($the_query -> post_count > 0){

        $services = array_map(function ($service){

            $case_type_h1_name = get_field('case_type_h1_name', $service);
            $image = get_field('case_type_main_image', $service);
            $thumb = $image['sizes']['medium'];
            $link = get_permalink();

            return (object)[
                'case_type_h1_name' => $case_type_h1_name,
                'thumb' => $thumb,
                'link' => $link

        } , $the_query -> posts);
return $services;

And here is the View Part there is a tag with a href that says href = {{$service -> link }} but nothing gets return

           <section id="section_5" class="my-5 bg-light py-5">
<div class="container-fluid">

  <div class="row text-center  justify-content-center">

@foreach( $services as $service)

    <div class="col-md-4 col-sm-4">
      <img src="{{ $service -> thumb }}" class="img-circle" alt="image" />
      <h2>{{ $service -> case_type_h1_name }} </h2>
      <p class="w-75 d-block mx-auto w-75 d-block mx-auto p-3">Vestibulum odio risus, interdum fringilla vulputate ac, placerat in mi. Vivamus pharetra, mauris vitae cursus ultricies.</p>
     <button class="btn w-25 advocatesbg text-white">
       <a href="{{$service -> link }} ">Read More</a></button>
    @if($loop->iteration % 3 === 0)
          <div class="row text-center  justify-content-center">



I guess to simplify my question is how do you get a custom post type link when calling it from the Controller.

Thanks in advance

I am guessing that get_permalink() returns the URL of the page that is running the controller?

Assuming that the get_field() calls returns the correct data when passing $service, try get_permalink($service).

1 Like

that was exactly what was happening, and yes I added get_permalink($service) and that totally worked. Thank you so much for your help

This topic was automatically closed after 42 days. New replies are no longer allowed.