Support

Account

Home Forums General Issues Upload PDF Manuals on all product pages and display all the uploaded value

Solving

Upload PDF Manuals on all product pages and display all the uploaded value

  • Hi,

    We have 300 products and every products contains a user manual. I created a field called pdf_upload where I’ve uploaded a pdf file on each individual product. The file can then be previewed on every single page.

    Here is my code:

    
    <?php $pdf_upload = get_field('pdf_upload');
        if( $pdf_upload ): ?>
           <a href="<?php echo $pdf_upload['url']; ?>">Manuel PDF</a>
        <?php endif; ?>
    

    Now, I want to create a page and display an ordered list of all uploaded PDF by date uploaded. So far, looking at acf documentation I’ve only been able to get the result of a field on a single product page but not all the results uploaded.

    How can I display a list of all the PDF’s that has been uploaded?

  • This is correct. There isn’t any way to get all of the values for all of the posts using any function in ACF or WP. To do what you want to do you would have to query the _postmeta table in the DB directly.

    https://developer.wordpress.org/reference/classes/wpdb/

    edit: in addition to this, this field in the DB only contains the ID value of the media file (upload) and you would need to get the link to this file yourself. https://developer.wordpress.org/reference/functions/wp_get_attachment_url/

  • HI @bigrat95

    Could you not just add a loop to your page template, loop through all pages/posts and output the ACF field?

    
    <?php
    $args = array(
    	'posts_per_page'	=> -1,
    	'post_type'			=> 'post',
     	'orderby' 			=> 'date',
        'order'   			=> 'DESC',	
    	'paged'				=> $paged,
    	'fields'			=> 'ids'			
    );
    $wp_query = new WP_Query($args);
    if ($wp_query->have_posts()) :
    	while ($wp_query->have_posts()) : $wp_query->the_post();
    
    	$pdf_upload = get_field('pdf_upload');
    	if( $pdf_upload ): ?>
           <a href="<?php echo $pdf_upload['url']; ?>">Manuel PDF</a>
        <?php endif; 
    
    	endwhile;
    endif;
    
  • @jarvis yes, this could be done. The only issue with it is that with 300 products it will be very slow. But it is a solution.

  • A possible alternative would be to use AJAX and create a load more function as the user scrolls down.

    Would be more work but would ease the page load!

    A good tutorial can be seen here.

  • @jarvis the loop suggested above dosen’t seems to be working. Am I missing something?

  • @bigrat95

    You may need to change the post_type and you will also need to amend the ACF field to the name of the one you set up.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.