• Resolved champdor

    (@champdor)


    I have a pod for downloadable resources with CFs file, authors and language. There are no single page for this pod, only the given list page. If you click on the title the downloadable pdf would load in a new tab.

    This behaviour stopped working. The loop does not generate download path for the pdf.
    What suspicious is that my text editor colors the $post->file differently than $post->authors or $post->language.

    Here is my loop item:

    
    <article class="sza_loop-item">
    	<div class="sza_icon">
    		<a href="<?php echo esc_url( $file ); ?>" target="_blank"><img src="/wp-content/uploads/2020/03/sza_pdf.svg"></a>
    	</div>
    	<div class="sza_content">
    		<?php if ( $post->authors ) {
    			echo __( 'Authors: ', 'rogerstheme' ) . esc_html( $post->authors ) . '<br>';
    				} ?>
    		<span class="sza_post-title"><?php echo '<a href="' . esc_url( $post->file ) . '"'; ?> target="_blank"><?php the_title() ?></a></span><span class="sza_lang-icon"><img height="16px" width="16px" src=<?php echo '"/wp-content/themes/rogerstheme/flags/' . esc_html( $post->language ) . '.svg"'; ?> alt=""></span><br><?php echo get_the_date( 'Y' ); ?> |<?php echo $output_tags; ?>
    	</div>
    </article>
    

    Questions:
    1. Is it posible that the $post->file is a system “variable” and I cannot use it for custom field name?
    2. If I rename it to dfile then will the CF values remain intact?

    Best regards,
    Peter

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    You shouldn’t use the object parameters to get custom fields. I’d advice to use get_post_meta, pods_field or pods_display functions.
    See: https://docs.pods.io/displaying-pods/
    Especially this page: https://docs.pods.io/displaying-pods/wordpress-theme-templates/

    Cheers, Jory

    Thread Starter champdor

    (@champdor)

    Dear Jory,

    1. The links you provided didn’t help. They are not covering my problem.

    2. I think get_post_meta() is too much because it returs a big array and I would have to pry the URL from it.

    3. I renamed the field to dfile and finally managed to do it with this code:

    
    $full_link = parse_url( $post->dfile["guid"] );
    $file_link = $full_link[path];
    

    This way when I go live I don’t have to bother with absolute paths.

    I know you suggested that I should not use object parameters but they worked for $post->authors and $post->language.

    But if you have any other suggestion how I could make it work better then I’m open for it.

    Regards,
    Peter

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hello @champdor

    The fact that object parameters work has nothing to do with the fact that they shouldn’t be used. This is merely a fallback option to fetch post meta but you cannot rely on this to work at all times, as you’ve noticed.

    Please, do actually fix your problem instead of changing names.

    2. I think get_post_meta() is too much because it returs a big array and I would have to pry the URL from it.

    This is incorrect. get_post_meta( $id, 'dfile' ) returns exactly the same as $post->dfile because there is no actual parameter dfile in the Post object. This is why the post object parses it as meta data internally.

    Also, the guid should never ever be used anywhere.

    In very short, the file field never stopped working. It’s the way you wrote your code that shouldn’t work at all. Please forgive me for being blunt with that statement.

    Steps to get the file:

    Option A: Using WordPress Core functions:

    1) $file = get_post_meta( $post->ID, 'file', true ) or get_post_meta( $post->ID, 'dfile', true ).
    Passing true as the third parameter will only return the single file data. This is how metadata works.

    2) Get the ID: $file_id = is_numeric( $file ) ? $file : $file['ID'];.

    3) Get the file URL: $file_url = wp_get_attachment_url( file_id );

    Option B: Using Pods functions (As seen in the doc I referred to in my first comment.):

    $file = pods_field( 'dfile._src' ); (note the ._src).

    Good luck!
    Cheers, Jory

    Thread Starter champdor

    (@champdor)

    Jory,

    The pods_field solution works like a charm.
    OK, I will never ever use guid ??
    Ever learning.

    Thank you for your time and support, I appreciate.
    Best,
    Champdor

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @champdor

    You’re welcome! The best way to say thanks is to leave a 5 star review at https://www.ads-software.com/plugins/pods/ and (if you’re feeling especially generous) become a Friend of Pods at https://friends.pods.io/

    Cheers, Jory

    Thread Starter champdor

    (@champdor)

    Hi Jory,

    I still have a problem, sorry for that.

    It turns out that the Pods solution above is not working on the English page, only on Hungarian (default language) page. It seems that neither of the two solution is giving back the dfile field value.

    A made a nice dump of the variables:
    https://r0g3rs.posdev.hu/en/szakmai-anyagok/

    My code looks like this:

    // Pods solution
    $pods_full_link = parse_url( pods_field( 'dfile._src' ) );
    $pods_file_link = $pods_full_link[path];
    
    // WP Core solution
    $file = get_post_meta( $post->ID, 'dfile', true );
    $file_id = is_numeric( $file ) ? $file : $file['ID'];
    $file_url = wp_get_attachment_url( $file_id );
    $full_link = parse_url( $file_url );
    $file_link = $full_link[path];

    The strange thing is that title, language CF, tags and year field is still working.

    Can you help in this?

    Regards,
    Champdor

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hello @champdor

    This is most likely an issue with WPML in syncing the metadata. Did you enable sync on that field? If so, disable it. The translation should link to the translated file, not the same.

    You could also wrap getting the dfile ID with this filter: https://wpml.org/wpml-hook/wpml_object_id/
    In that case you should use the WP core solution.

    Cheers, Jory

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom field “file” stopped working’ is closed to new replies.