Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8571253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:44:31+00:00 2026-06-11T18:44:31+00:00

I have created a custom post type for my portfolio page. I have two

  • 0

I have created a custom post type for my portfolio page. I have two custom fields:

  1. Product URL
  2. Case Study URL

My issue is using the values of these custom fields in my theme. Currently I fetch their values by running this:

<?php 
$loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 3)); 
while ( $loop->have_posts() ) : $loop->the_post();

$custom = get_post_custom($post->ID);
$product_url = $custom['product_url'];
foreach ($product_url as $key => $value)
echo $key . " => " . $value . "<br />";

$custom = get_post_custom($post->ID);
$case_study_url = $custom['case_study_url'];
foreach ($case_study_url as $key => $value)
echo $key . " => " . $value . "<br />";

?>

// All my mark-up

<?php endwhile; ?>

Idk if my terminology will make sense here, but isn’t there a way I could run the foreach loop so that it displays the product_url and the case_study_url?

Thanks in advance for your help.

Sincerely.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:44:32+00:00Added an answer on June 11, 2026 at 6:44 pm

    If you’re only retrieving two values, there’s no reason to use get_post_custom. Consider using Get Post Meta instead:

    <?php 
    $loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 3)); 
    while ( $loop->have_posts() ) : $loop->the_post();
    
    $product_url = get_post_meta($post->ID, 'product_url', true);
    $case_study_url = get_post_meta($post->ID, 'case_study_url', true);
    echo "Product: $product_url, Case Study: $case_study_url";
    ?>
    
    // All my mark-up
    
    <?php endwhile; ?>
    

    UPDATE:

    get_post_custom is better when dealing with meta-fields that aren’t specific to the structure of your template. So if you want to give the user the ability to add their own fields and have them show up on the page without actually specifying any keys yourself, you can do something like this:

    <?php 
    $loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 3)); 
    while ( $loop->have_posts() ) : $loop->the_post();
    
    $all_meta = get_post_custom($post->ID);
    foreach($all_meta as $meta_key => $meta_value)
        echo "$meta_key: $meta_value<br/>";
    
    endwhile;
    ?>
    

    Bear in mind that when setting values that utilize the same key, the result will be of type array.

    So let’s say you have a Custom Field called “Foo”, and you set it to “Bar”. Then you have another custom field called “Foo” and set that one to “Baz”. When you do this:

    <?php
    $loop = new WP_Query(array('post_type' => 'portfolio', 'posts_per_page' => 3)); 
    while ( $loop->have_posts() ) : $loop->the_post();
        $foo = get_post_meta($post->ID, 'Foo');
        print_r($foo);
    endwhile;
    ?>
    

    You will get a series of values, rather than just a simple string since two values utilize the same key. By setting the third parameter to ‘true’ in get_post_meta (as seen in our above example), you are specifying that the return value must be a string.

    Just play around with it, and you’ll see what I mean. Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom post type of 'portfolio' and page with a template
I have created a custom post type named People. I have created a page
I have created a wordpress custom post type. I am using custom field template
i have created a custom post type named Books & i have a widget
I have created a custom post type called banners and want to enable the
Using Marius Gedminas's excellent blog post , I have created a custom traverser for
I have created a custom post type calles articles and a custom category like
I have created a custom post type - Testimonials - for a WP site
I have created a custom post type restaurant. But Its not coming on the
I have a WordPress custom post-type setup. I've created single-[customposttype].php However instead of displaying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.