1
votes

I am trying to create a function for a shortcode to loop through an 'articles' custom post type. This is essentially a downloadable file uploaded by users and is a custom post type without the content field/editor box in the post editor. This custom post type also has a 'download_link' ACF field attached to it which is the location of the file for downloads. This is the href attribute for each post in the feed. However, the field is not generating any output in the function below. However the title is being populated. I know that all the article posts have a value provided for this particular field. Am I missing something? Thanks.

// CREATE A FEED OF ARTICLES WITH LINKS
function articles_download_feed(){
    $articles_query = new WP_Query(array('post_type' => 'article', 
        'post_status' => 'any'));
        if( $articles_query->have_posts() ){
            $output = '<ul>';
            while( $articles_query->have_posts() ){
                $articles_query->the_post();
                $articleID = get_the_ID();
                $title = get_the_title($articleID);
                if(get_field('download_link', $articleID)){
                    $download = get_field('download_link', $articleID, true);
                } else {
                    $download = 'ABC';
                }
                $output .= '<li><a target="_blank" href="'.$download.'">'.$title.'</a></li>';
        }
    $output .= '</ul>';
    wp_reset_postdata();
    return $output;
    } 
} add_shortcode('articles_feed', 'articles_download_feed');
1
Try using a setup_postdata(); function on the first line of the while loop. - Gavin Thomas
you realise because you're inside a post loop you dont need to pass the ID with the get field, also not sure why you have "true" in your get_field(); as that isn't needed perhaps this is what's breaking your code. Also it's probably worth not have ABC as your fallback, use # or just leave it blank so the link won't work. - Daniel Vickers

1 Answers

0
votes

I managed to fix this issue myself.

I set up the 'dile_dl_link' custom field for 'article' type posts via the add_post_meta() WP function, instead of via Advanced Custom Fields (below is the segment of code to create a new article):

// CREATE NEW ARTICLE LINK FROM GRAVITY FORM SUBMISSION
function article_upload_createlink($entry, $form){
    // Fields
    $post_title = ucwords($entry[8]);
    $post_status = 'publish';
    $post_excerpt = $entry[9];
    $post_type = 'article';
    $dl_link = $entry[3];
    // Post being created
    $new_article = array(
      'post_title' => $post_title,
      'post_status' => $post_status,
      'post_excerpt' => $post_excerpt,
      'post_type' => $post_type, 
    );
    // ID for post
    $theId = wp_insert_post($new_article);
    // Update download_link
    add_post_meta($theId, 'file_dl_link', $dl_link);
} add_action( 'gform_after_submission_7', 'article_upload_createlink', 10, 2 );

The function in question as presented in the question has been changed as seen below:

// CREATE A FEED OF ARTICLES WITH LINKS
function articles_download_feed(){
    $articles_query = new WP_Query(array('post_type' => 'article', 'post_status' => 'any'));
    if( $articles_query->have_posts() ){
        $output = '';
        while( $articles_query->have_posts() ){
            $articles_query->the_post();
            $articleID = get_the_ID();
            $title = get_the_title();
            $excerpt = get_the_excerpt();
            $get_dllink = get_post_meta($articleID, 'file_dl_link', true);
            $output .= '<div class="wpb_wrapper">
                    <div class="vc_empty_space" style="height: 24px">
                        <span class="vc_empty_space_inner"></span>
                    </div>
                    <div class="wpb_text_column ">
                        <div class="wpb_wrapper">
                            <h4>'.$title.'</h4>
                            <p>'.$excerpt.'</p></div>
                        </div>
                        <div class="w-btn-wrapper align_left">
                            <a target="_blank" class="w-btn style_raised color_primary icon_none" href="'.$get_dllink.'"><span class="w-btn-label">DOWNLOAD</span><span class="ripple-container"></span></a>
                        </div>
                    </div>
                    <div class="w-separator type_default size_medium thick_1 style_solid color_border cont_none">
                        <span class="w-separator-h"></span>
                    </div>';
        }
        wp_reset_postdata();
        return $output;
    }
}
add_shortcode('articles_feed', 'articles_download_feed');