0
  • I'm using Advanced Custom Fields.
  • I have a registered bookitall_roomstype custom post type.
  • I have a registered bookitall_bookings as another custom post type.
  • In the bookitall_bookings there is a field called bookitall_roomstype that is stored as a pagelink (acf "type"). (This looks like a permalink to the actual roomtype (It's NOT stored as a title/name)

I'm setting up certain columns (and their values) to be shown. Now I want the roomtype's name to be shown. I'm doing like below...

This is a snippet of my code. This snippet retrieves the NAME roomtype_name of the pagelink-type bookitall_roomstype:

$allfields_currentpost = get_fields();
$args = array(
          'posts_per_page'   => -1,
          'offset'           => 0,
          'orderby'          => 'ID',
          'order'            => 'ASC',
          'post_type'        => 'bookitall_roomtypes'
);
$roomtypes = get_posts( $args );  

$pagelink = get_field('bookitall_roomstype'); //pagelink acf type        
$roomtypes_slugs = array();
foreach ( $roomtypes as $r ) {
    $roomtypes_slugs[] = get_permalink( $r->ID);
}
$arrkey = (array_search($pagelink, $roomtypes_slugs));
$roomtype_name = '';
if ( $arrkey !== false ) {
    $roomtype_name = $roomtypes[$arrkey]->post_title;
}      

The above do actually work (I put it here if someone have the same issue and I also want to know if there is a better/simpler way of achieving above? (getting the roomtype's name from current post)

I've tried to use url to postid( $pagelink ) and get_page_by_path( $pagelink ) but those only returns 0 (which means no object is returned). If a post had been returned I could use $post->post_title.

UPDATE: New code (thanks to @Milo !!!):

$allfields_currentpost = get_fields(false, false);        
$roomtype_id = $allfields_currentpost['bookitall_roomstype'];  
$roomtype_name = get_the_title($roomtype_id);

(I'm jusing the other field values from the current post as well, therefore I'm using get_fields() instead of get_field())

1

The page link field actually stores the post ID, which gets formatted internally by the get_field function on output. The 3rd argument for get_field lets you disable that output formatting so you just get the ID back, which you can then use to get title and permalink for that post:

$post_id = get_field( 'bookitall_roomstype', false, false );
if( $post_id ):
    echo get_the_permalink( $post_id );
    echo get_the_title( $post_id );
endif;
  • Thank you a lot! Did not know that about the output options. IMO false should be default value, but I guess you can't get it all ;-) (ACF is fantastic) – bestprogrammerintheworld Jun 29 '18 at 17:37
1

You shouldn't need to get all bookitall_roomstype in lines 2-9 of your code. You really only want to pull room types that are assigned to the current bookitall_bookings CPT.

As far as the "pagelink" field, this seems like an unsafe way of calling the information. What if a permalink changed someday? It seems like it would be both safer and more efficient to save post IDs instead of permalinks as the bookitall_roomtypes field.

With that in mind, and also to make it clearer bookitall_roomstype CPT name is not the same as bookitall_roomtypes custom field, I would suggest something like this:

// pull an ACF field that has the post ID saved
$room_type_ids = get_field('bookitall_room_type_ids');
// create an empty array to hold room type details
$room_types = array();
foreach($room_type_ids as $room) {
    // save the permalink
    $room_types[][0] = get_the_permalink($room);
    // save the name
    $room_types[][1] = get_the_title($room);
}
// now you can loop through the room types to display as you wish
// this just links the name to the permalink, but you can add whatever
// markup you need
foreach($room_types as $room_details) {
    echo '<a href="' . $room_details[0] . '">' . $room_details[1] . '</a>';
}

This way you're only pulling the minimum information required, and not every single roomtype, followed by each roomtype's details, only then to check whether you need those particular details for the current post or not. :)

  • Actually I need the other fields as well of the CPT (sorry for not making that clear). I only need one specific roomtype (the select roomtype for the booking) and not a list of the roomtypes. Your code will not work when appending $room_types[][0] and then appending $room_types[][1] in the same iteration. .(there will be two items where the first item has key 0 and the second item in the array has the key 1). I do not understand what you mean it's not safe. It's just a comparision? Even I change the permalink it will work. – bestprogrammerintheworld Jun 29 '18 at 17:33
  • Sorry. I accidently put my updated code (for review) into your posting here. You can remove it if you want :-) – bestprogrammerintheworld Jun 29 '18 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.