0

I have create a custom post object with custom post metas.

Like this:

$post->custom_meta = get_post_meta();

$custom_post = $post;

Am I how to define the $custom_post object to global in master level?

Can I request any file it like single.php

global $custom_post;

the_title();

echo $custom_post->custom_meta;

or index.php

global $custom_post;

// The Loop
if ( $custom_post->have_posts() ) {
    echo '<ul>';
    while ( $custom_post->have_posts() ) {
        $custom_post->the_post();
        echo '<li>' . get_the_title() . '</li>';
        echo $custom_post->custom_meta;
    }
    echo '</ul>';
} else {
    // no posts found
}

How to set $custom_post an global and in where? (functions.php?) thx.

2
  • 3
    Why would you want to do that when you could simply call get_post_meta() in your loop (and basically, everwhere in WordPress - templates, template parts, etc).
    – Sally CJ
    Jan 12, 2021 at 9:15
  • 1
    global is not good practice in PHP anymore. Have you looked into other more recent programming patterns to solve this?
    – kero
    Jan 12, 2021 at 9:56

0

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.