I have a function in php as shown below:
function hello_world( $post ) {
echo '<pre>'; print_r($post); echo '</pre>'; // Line A
echo '<pre>'; print_r($post->post_meta); echo '</pre>'; // Line B
return $post->post_meta;
}
I have added Line A
and Line B
in the function above for debugging purposes. In the function hello_world
above, Line A
returns list of all posts whereeas Line B doesn't return anything.
I am wondering what changes I need to make in the wordpress or php code above so that Line B
returns list of posts.
Edit 1:
Added code for $loc_query
function area_search( $abc ) {
if ( $abc ) {
$loc_query = new \WP_Query( [
's' => $abc,
'post_type' => 'abc-xyz',
'post_status' => 'publish',
] );
} else {
$loc_query = new \WP_Query( [
'post_type' => 'abc-xyz',
'post_status' => 'publish',
'ep_integrate' => true,
'orderby' => 'title',
'order' => 'ASC',
] );
}
$result = array_map( __NAMESPACE__ . '\hello_world', $query->posts );
echo '<pre>'; print_r($result); echo '</pre>'; // Line A
return group( $result );
}
I have a function in php as shown below:
function hello_world( $post ) {
echo '<pre>'; print_r($post); echo '</pre>'; // Line A
echo '<pre>'; print_r($post->post_meta); echo '</pre>'; // Line B
return $post->post_meta;
}
I have added Line A
and Line B
in the function above for debugging purposes. In the function hello_world
above, Line A
returns list of all posts whereeas Line B doesn't return anything.
I am wondering what changes I need to make in the wordpress or php code above so that Line B
returns list of posts.
Edit 1:
Added code for $loc_query
function area_search( $abc ) {
if ( $abc ) {
$loc_query = new \WP_Query( [
's' => $abc,
'post_type' => 'abc-xyz',
'post_status' => 'publish',
] );
} else {
$loc_query = new \WP_Query( [
'post_type' => 'abc-xyz',
'post_status' => 'publish',
'ep_integrate' => true,
'orderby' => 'title',
'order' => 'ASC',
] );
}
$result = array_map( __NAMESPACE__ . '\hello_world', $query->posts );
echo '<pre>'; print_r($result); echo '</pre>'; // Line A
return group( $result );
}
Share
Improve this question
edited Jul 6, 2020 at 18:59
user1950349
asked Jul 6, 2020 at 18:26
user1950349user1950349
1012 bronze badges
16
|
Show 11 more comments
1 Answer
Reset to default 1Your code is interfering with the internal structure of WP_Query
and WP_Post
, and making assumptions about how it works. This is extremely unusual, and not best practice when developing with WordPress.
Case in point, officially there is no post_meta
member variable on that class ( https://developer.wordpress/reference/classes/wp_post/ ). That's not how you fetch a posts meta key/values.
Instead, use a standard post loop, and standard API calls.
Here's what a standard WP_Query
post loop should look like:
$args = [
// parameters go here
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// display the post
the_title();
the_content();
}
wp_reset_postdata();
} else {
echo "no posts were found";
}
Note that a standard post loop lifecycle happens, as well as all the expected hooks. You can do work inside the while
loop for each post. For example, fetching all the post meta. To do that, use the get_post_meta
function:
$all_meta = get_post_meta( get_the_ID() );
It might be tempting to think that a WP_Post
object follows the naive OO dream of an all encompassing API for posts, a one-stop representation of a single post and all the things that there is to know and can be done about it. But in reality it's a data container. You won't find class methods or methods of sub-classing it.
Additionally, by poking around and directly accessing the internal data structures, you bypass a lot of functionality and risk your code being broken in the future. For example post meta gets fetched in advance and stored in WP_Cache
to avoid query duplication. There are also filters that allow plugins and other code opportuniities to make changes or fix things.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742287510a4415589.html
$result = array_map( __NAMESPACE__ . '\hello_world', $loc_query->posts );
– user1950349 Commented Jul 6, 2020 at 18:38$loc_query
and the arguments ? – Sabbir Hasan Commented Jul 6, 2020 at 18:55$post->post_meta
, that's not how you should be retrieving post meta. Useget_post_meta
– Tom J Nowell ♦ Commented Jul 6, 2020 at 19:21get_post_meta
is a function, it isn't a method on the post object. I've just looked at your edit and you don't use a normal post loop, call none of the standard loop functions, and appear to be directly using the internals ofWP_Query
, all very unusual. What exactly are you trying to do that required this code? This looks like an X Y question, where you asked how to implement your solution, when you should have asked how to solve your problem – Tom J Nowell ♦ Commented Jul 6, 2020 at 19:24