Last active
January 5, 2020 18:13
-
-
Save pingram3541/c057977d7afa7d4036d674e929d5f614 to your computer and use it in GitHub Desktop.
Shotcode to create custom url from ACF fields and post id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
V2 updated to show additional loop based on current cpt post's ACF field containing a string of other post IDs.