Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of a custom field of a WordPress post

Tags:

php

wordpress

I have created one custom field in WordPress' edit post section, and am able to save the value by following this posts: Wordpress - Adding custom field to the post screen.

I'm able to retrieve the posts of the specific category, but I'm unable to retrieve the value of custom field.

enter image description here

As seen in image above, there is a custom post field at highlighted at the top left. The other highlighted field is to show that the post belongs to the "Portfolio" category.

Here is the code I used to retrieve the posts of the category "Portfolio"

<?php 
    $the_query = new WP_Query(array(
    'category_name' => 'Portfolio', 
    'posts_per_page' => 9,
    'order' => 'DESC'
)); 
while ( $the_query->have_posts() ) : 
    $the_query->the_post();
?>

<p>The title: <?php the_title(); ?></p>
<p> custome value: <?php get_post_meta( $post_ID, '_ssb_portfolio_url', true); ?> </p>
<p>The Content: <?php the_content(); ?></p>

<?php 
    endwhile; 
    wp_reset_postdata();
?>

I'm able to get the value of the title of the post and the content of the post, but not custom field value. What wrong in my code?

like image 607
Arish Avatar asked Feb 18 '23 02:02

Arish


1 Answers

You can use get_post_custom( $post_id )

In your case

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    $custom = get_post_custom( get_the_ID() ); ?>

Then $custom is an array of your custom fields

like image 113
Galen Avatar answered Feb 19 '23 14:02

Galen