Skip to content

Instantly share code, notes, and snippets.

@pingram3541
Last active January 5, 2020 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pingram3541/c057977d7afa7d4036d674e929d5f614 to your computer and use it in GitHub Desktop.
Save pingram3541/c057977d7afa7d4036d674e929d5f614 to your computer and use it in GitHub Desktop.
Shotcode to create custom url from ACF fields and post id
/**
* Returns a custom url based on the post id, title and a unique ACF field value then loops through
* the resulting string of id's returned by "this" post's ACF field, querying and returning yet another ACF
* field from each looped post id.
**/
add_shortcode( 'url_from_acf', 'my_url_from_acf' );
function my_url_from_acf(){
$post_id = get_the_ID();
if ( get_post_type( $post_id ) === 'my_cpt' ) {
$my_title = get_the_title( $post_id );
$my_post_ids_string = get_field( 'acf_field_with_post_ids' );
$my_post_ids = explode(',', $my_post_ids_string);
$my_post_ids_array = [];
foreach( $my_post_ids as $url_post_id ){
$my_url_path = get_field( 's3_video_file_path', $url_post_id, false );
array_push( $my_post_ids_array, $my_url_path );
}
$playlist_filelist = implode(',', $my_post_ids_array );
$url = 'https://mysite.com/?my_title='. urlencode($my_title) .'&my_post_ids_playlist='. $playlist_filelist .'#my_scroll_to_achor';
return $url;
}
}
@pingram3541
Copy link
Author

V2 updated to show additional loop based on current cpt post's ACF field containing a string of other post IDs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment