1

I need to get the attachment (will be only one per post) associated to a post with meta_key wpcf-legislacion-gratis (not matter the value the column has) by using a custom query using get_posts() function. I've tried this two ways:

$nonActivePlan_args   = array(
    'order'           => 'desc',
    'meta_query'          => array(
            array(
                'key'     => 'wpcf-legislacion-gratis',
                'value'   => NULL,
                'compare' => '!='
            )
    ),
    'post_status'         => 'inherit',
    'posts_per_page'      => 1,
    'post_type'   => 'attachment',
    'post_parent' => get_the_ID(),
);

$nonActivePlanAttachment = get_posts( $nonActivePlan_args );
if ($nonActivePlanAttachment) {
    foreach ($nonActivePlanAttachment as $attachment) { ?>
        <div class="legislacion-ico">
            <a class="pdf" href="<?php echo wp_get_attachment_url( $attachment->ID, true ); ?>">
                <img src="<?php echo get_template_directory_uri(); ?>/images/ico_descargas.png">
                <br>
                Descargar PDF
            </a>
        </div>
    <?php
    }
}

wp_reset_postdata();

Related to the one above I have this doubts:

  • How do I write the query for find the attachment with meta_key equal to wpcf-legislacion-gratis not matter what values it has?
  • Is the loop on this case the right one? Notice I'll only have one wpcf-legislacion-gratis per post so do I need to loop over the results? Shouldn't be just one?
  • If I'll get both attachment separated (as you will see in the query at the end of the post) should I execute wp_reset_postdata()?

I've tried also this code:

$nonActivePlan_args   = array(
    'order'             => 'desc',
    'meta_key'          => 'wpcf-legislacion-gratis',
    'post_status'       => 'inherit',
    'posts_per_page'    => 1,
    'post_type'         => 'attachment',
    'post_parent'       => get_the_ID(),
);

But is not working meaning I don't get any value from DB. This is the result of query I've executed on the MySQL command line:

SELECT * FROM `wp_postmeta` where post_id='11839';
+---------+---------+-------------------------+-----------------------------------------------------------------------------------+
| meta_id | post_id | meta_key                | meta_value                                                                        |
+---------+---------+-------------------------+-----------------------------------------------------------------------------------+
|   22149 |   11839 | wpcf-legislacion-gratis | http://jurisprudencia.dev/wp-content/uploads/2015/02/modelodatos.pdf              |
|   22150 |   11839 | wpcf-legislacion-pagada | http://jurisprudencia.dev/wp-content/uploads/2015/02/AtencionUsuario_version1.pdf |
+---------+---------+-------------------------+-----------------------------------------------------------------------------------+

I have also checked docs here and here but didn't found any helpful to me.

How do I get the attachment? Any help or advice?

4
  • Is this on a single post page Mar 6 '15 at 5:26
  • @PieterGoosen Yes, it's in a single-legislacion.php
    – ReynierPM
    Mar 6 '15 at 5:29
  • Any reason why you are running a custom query instead of using get_post_meta() Mar 6 '15 at 5:31
  • @PieterGoosen not at all, can you provide an example? How to get the same but using get_post_meta() instead?
    – ReynierPM
    Mar 6 '15 at 5:34
1

I think what you are after is get_post_meta() and not a custom query.

REASONS:

  • The results from custom fields are cached and extremely streamline when it cone to performance. You can read this answer I have done recently which will explain everything

  • You are trying to get the custom field attached to a post only in a single post page. This is what get_post_meta() was designed for

You can replace all your code with something like this: (Modified from the codex)

$key_1_value = get_post_meta( get_the_ID(), 'wpcf-legislacion-gratis', true ); 
// check if the custom field has a value 
if( ! empty( $key_1_value ) ) { 
    echo $key_1_value; 
} 

EDIT

If you need to get the custom field value outside the loop like in a sidebar or function, you need to replace get_the_ID() with get_queried_object_id()

$key_1_value = get_post_meta( get_queried_object_id(), 'wpcf-legislacion-gratis', true ); 
// check if the custom field has a value 
if( ! empty( $key_1_value ) ) { 
    echo $key_1_value; 
} 
3
  • Ups, Pieter my bad I just notice that is not in a single, it's in a sidebar-legislacion.php, I think in this environment your code will not works, I forgot to mention that on the main post, any advice taking this mistake from me into account?
    – ReynierPM
    Mar 6 '15 at 6:02
  • Ok, just to be clear then, you need to get the custom field value of the post on a single post page in the sidebar of the single page Mar 6 '15 at 6:09
  • See my update on usage outside the loop Mar 6 '15 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.