0

I use a custom WP_Query to load image and other information from custom post type event to supersized.js script. This query get all custom post type event with custom field wpcf-img-event not empty and with a custom field _event_start_date greater than today.

From each post i get the url of the image specificed in the custom field mentioned above using get_post_meta(); but this return the url of original image. So to use the image with a custom size i use a custom function pn_get_attachment_id_from_url(); (declared inside funtions.php) that extract the id attachment from a url. Then i use wp_get_attachment_image_src();

I would like to share my code but i note that pn_get_attachment_id_from_url(); use another query so i would aks:

1) if this thing is a bad choise and if is there a better method to do it.

2) i also have to grab the name of location associated to custom post type event.

Location is another custom post type associated to custom post type event (i can select it by dropdown list metabox of custom post type event). with get_post_meta($postID, '_location_id', true); i can get the id. but if i would like to get its custom field value (city, adress, ecc...) from it have i to use another query? (get custom field from custom posty type associated to another custom post type)

This my WP_Query

<?php
// Don't Populate list if no Featured page is set
if ( $featured_page <>"" ) { 
    $today = date ('Y-m-d');
    $arg = array (
        'post_type' => 'event',
        'meta_key'  => 'wpcf-img-event',    
        'posts_per_page' => '2',
        'post_status'    => 'publish',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'wpcf-img-event',
                'value' => '',
                'compare' => '!='
            ),
            array(
                    'key' => '_event_start_date',
                    'value' => $today,
                    'compare' => '>='
            )
        ),
    );
    $futurevent = new WP_Query($arg);

    // Loop through the custom post type 'event'
    if ($futurevent->have_posts()) : 
        while( $futurevent->have_posts() ) : $futurevent->the_post();
            global $post;
?>
            <!-- Cosa fare dentro il loop -->
<?php
            $postID = $post->ID;

            $imageURI = get_post_meta($postID, 'wpcf-img-event', true);

            $attachmentID = pn_get_attachment_id_from_url ($imageURI);
            $imagearray = wp_get_attachment_image_src( $attachmentID, 'fullsize');
            $imageURI = $imagearray[0];

            $thumbarray = wp_get_attachment_image_src( $attachmentID, 'fullscreen-thumbnails');
            $thumb_imageURI = $thumbarray[0];

            $eventTitle = get_the_title($postID);

            $eventCap = get_post_meta($postID, 'wpcf-event-caption', true);
            // $eventCap = utf8_truncate($eventCap);

            $postlink = get_permalink($postID);


            $eventCap = wp_trim_words( $eventCap, $num_words = 2, '<a href="'. get_permalink() .'"> ...Read More</a>' );
            // If linking is On
            //if ($featured_linked == 1 || $featured_linked == true) {
            //  $attatchmentURL = get_attachment_link($image->ID);
            //}
            // Count
            $count++;
            if ($count>1) { echo ","; }

            $slideshow_title = "";
            $slideshow_caption = "";

            //Find and replace all new lines to BR tags
            $find   = array("\r\n", "\n", "\r");
            $replace = '<br />';
            $eventCap = str_replace($find, $replace , $eventCap);

            if ($eventTitle) $slideshow_title='<div class="slideshow_title">'. esc_attr($eventTitle) .'</div>';
            if ($eventCap) $slideshow_caption='<div class="slideshow_caption">'. $eventCap .'</div>';

            echo "{image : '".$imageURI."', title : '". $slideshow_title . $slideshow_caption . "', thumb : '" . $thumb_imageURI . "', url : ''}";
        endwhile;
    else: 
?>    
        <!-- Cosa fare se il loop non trova niente -->
        <p>?><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php
    // If Ends here for the futurevent
    endif;
    wp_reset_postdata();

// If Ends here for the Featured Page
}
?>

and this is the custom funtion (original code https://philipnewcomer.net/2012/11/get-the-attachment-id-from-an-image-url-in-wordpress/)

<?php
function pn_get_attachment_id_from_url( $attachment_url = '' ) {

    global $wpdb;
    $attachment_id = false;

    // If there is no url, return.
    if ( '' == $attachment_url )
        return;

    // Get the upload directory paths
    $upload_dir_paths = wp_upload_dir();

    // Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
    if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {

        // If this is the URL of an auto-generated thumbnail, get the URL of the original image
        $attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );

        // Remove the upload path base directory from the attachment URL
        $attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url );

        // Finally, run a custom database query to get the attachment ID from the modified attachment URL
        $attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );

    }

    return $attachment_id;
}
?>
  • Please do not link to outside sources for code. Put everything that is needed in a question inside the question. Also: If you have not written the code yourself, please link to its origin and tell up its license. Thank you. – kaiser Jun 19 '14 at 22:05

Your Answer

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

Browse other questions tagged or ask your own question.