• Resolved Mike_sa

    (@mike_sa)


    Hi
    I am having an issue with calling the custom field values from each custom post type.
    I have created a custom post type “rankings ads”. In each post made within the custom post type has a custom field that needs to be entered. It is the url for the advertiser.

    At the moment my array to call the images from the various custom posts is working but I am not able to call the custom field values for each of the custom posts.

    Unfortunately I cannot supply a url because the site is hidden behind a under construction page.
    Below is the code function calling the array :

    <br />
    //u19 IPT ranking tournament ads top
    function rank_u19_ad_top() {</p>
    	// Call Sponsors of WPYS
    	// add call for query
    						$args = array('orderby' => 'date',
    						'order' => 'ASC',
    						'posts_per_page' => 10,
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'rankings',
    			'field' => 'slug',
    			'terms' => 'rank-u19-ipt',
    		)
    	)
    );
    $query = new WP_Query( $args );
    // The Loop
    
    	$output = '';
    		while ( $query->have_posts() ) : $query->the_post();
    			$output.='<div class="add-top">';
    			$output.='<a href="';
    			$output.= $ad_url = get_post_meta($post->ID, 'ad-url', true);
    			$output.='" rel="nofollow">';
    			$output.= get_the_post_thumbnail();
    			$output.='';
    			$output.='</div>';
    		endwhile;
    			echo $output;
    // Reset Post Data
    wp_reset_postdata();
    }

    The bit that is not calling the custom field data for each post is :

    $output.= $ad_url = get_post_meta($post->ID, 'ad-url', true);

    The function is then displayed on another post.

    Could someone please point me in the correct direction, I have not been able to find a solution in google.

    Many thanks

    This is urgent ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Mike_sa

    (@mike_sa)

    I should add that the custom field values are not being set on the page that the array is being displayed, they being held on each of the custom post.

    The same custom field name is being used on each post….they all require ad-url to be set.

    I also see in the above code , for some reason it has added some html that I do not have in my code…please ignore it ??

    Two things are going on in your function that you’ll want to fix. The first that $post doesn’t exist in the function’s scope. You’re also including an unnecessary variable assignment when you fetch the post meta field. So, instead of this line:
    $output.= $ad_url = get_post_meta($post->ID, 'ad-url', true);
    Try this line:
    $output.= get_post_meta(get_the_ID(), 'ad-url', true);

    Thread Starter Mike_sa

    (@mike_sa)

    Thanks Andrew.
    I have managed to sort it.

    Just got into a panic with the deadline looming. Luckily I managed to get it with the help of a friend ??

    Below is the working example (functions names may have changed but the content of the function is all working ) :


    //u19 IPT ranking tournament ads top
    function rank_u19_ad_top( $echo = true ) {

    global $post;

    // Call Sponsors of WPYS
    // add call for query
    $args = array(
    'orderby' => 'date',
    'order' => 'ASC',
    'posts_per_page' => 10,
    'tax_query' => array(
    array(
    'taxonomy' => 'rankings',
    'field' => 'slug',
    'terms' => 'rank-u19-ipt'
    )
    )
    );
    $query = new WP_Query( $args );

    //Test For Found Posts
    if( ! $query->have_posts() )
    return false;

    // The Loop
    //must have the first output before the loop begin or it will only return the last data. Remeber to also use get_ because it is not passing the data straight to the browser like with echoing :)
    $output = '';

    while ( $query->have_posts() ) : $query->the_post();

    //Get The AD URL
    $ad_url = get_post_meta( $post->ID, 'ad-url', true );

    $output.='<div class="add-top">';
    $output .= '' . get_the_post_thumbnail( $post->ID ) . '';
    $output.='</div>';

    endwhile;

    // Reset Post Data
    wp_reset_postdata();

    if( ! $echo )
    return $output;

    echo $output;

    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Call custom field values in array of custom post type posts’ is closed to new replies.