metabox - get_post_meta not working on my custom-function page

admin2025-06-03  4

I have saved the meta key with the name writer_meta_key and want to retrieve it on my custom-functions page. I can get it on front page and single page but it doesn't work on my custom-function page. Here is my code. It gives only title but not the writer's name.

$term       = get_queried_object();
$tax_object = get_taxonomy( $term->taxonomy );
$post_type_array = $tax_object->object_type;
$post_type  = $post_type_array[0];

$posts      = get_posts( array(
    'post_type' => $post_type,
    'taxonomy'  => $term->taxonomy,
    'term'      => $term->slug
) );

foreach ($posts as $post) {
    $id = get_the_ID();
    $custom_meta_writer = get_post_meta( $id, 'writer_meta_key', true );       
    $writer = $custom_meta_writer['writer'];
    echo $post->post_title; 
    echo $writer; 
}

When I echo $id it shows error as

Array
Array
Array
Array

Where have I mistaken??

I have saved the meta key with the name writer_meta_key and want to retrieve it on my custom-functions page. I can get it on front page and single page but it doesn't work on my custom-function page. Here is my code. It gives only title but not the writer's name.

$term       = get_queried_object();
$tax_object = get_taxonomy( $term->taxonomy );
$post_type_array = $tax_object->object_type;
$post_type  = $post_type_array[0];

$posts      = get_posts( array(
    'post_type' => $post_type,
    'taxonomy'  => $term->taxonomy,
    'term'      => $term->slug
) );

foreach ($posts as $post) {
    $id = get_the_ID();
    $custom_meta_writer = get_post_meta( $id, 'writer_meta_key', true );       
    $writer = $custom_meta_writer['writer'];
    echo $post->post_title; 
    echo $writer; 
}

When I echo $id it shows error as

Array
Array
Array
Array

Where have I mistaken??

Share Improve this question edited Jan 31, 2019 at 3:45 Howdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges asked Jan 30, 2019 at 18:23 JklynJklyn 1035 bronze badges 6
  • Welcome to WPSE. Where is your custom-function page used? Is it an include file called by a page template? – jdm2112 Commented Jan 30, 2019 at 19:04
  • It's included in a folder inc and is called in functions.php – Jklyn Commented Jan 30, 2019 at 19:09
  • It is important to remember that WordPress is an application that runs when a user requests content and does nothing otherwise (over-simplified) so my question is related to that. What triggers the code above to actually run? Typically custom functions (or plugins) are fired when the action they are hooked to is called. – jdm2112 Commented Jan 30, 2019 at 19:13
  • First of all I'ven't used any plugins in this project. This is just a page where I've done all the codings. The above code displays the category posts and what I need is to retrive the meta key of the title. – Jklyn Commented Jan 30, 2019 at 19:20
  • To simplify the question - how does this code get called? It isn't in a function hooked to an action. It is not part of a page template. How would WordPress know this code exists? – jdm2112 Commented Jan 30, 2019 at 19:30
 |  Show 1 more comment

1 Answer 1

Reset to default 0

WordPress uses a global $post to keep track of the current post the user is viewing. Whenever you call functions like the_title() or get_the_ID() it uses that global $post to pull that data. In your code snippet you're using get_posts() which generally does not overwrite the global $post object unless you specify with setup_postdata( $post ).

The easiest thing to do is to simply modify your metadata line and use:

$custom_meta_writer = get_post_meta( $post->ID, 'writer_meta_key', true );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748959290a315165.html

最新回复(0)