Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 212 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
WordPress: how to return meta with query_posts?

#1
I'm doing an AJAX request using admin-ajax.php whereby I filter posts based on which check-box is checked. It's working great, although I'm struggling to find a way to return the meta details of each post.

I'm just using query_posts to get my data as below:

function ajax_get_latest_posts($tax){

$args= array(
'post_type'=>'course',

'tax_query' => array(
array(
'taxonomy' => 'subject',
'field' => 'slug',
'terms' => $tax
)
)

);

$posts=query_posts( $args);


return $posts;
}

How would I modify this to also return meta data? I know I can filter the posts by meta data using meta_query, but I just want to display the data in my posts.
Reply

#2
EDIT:

Besides the solution outlined bellow, if you're using WordPress >= 3.5(as you should be :) ), you can simply make use of the magic methods of the WP_Post object.

Basically the WP_Post object(which is what the posts array from pretty much every query result that comes from WP_Query consists of) is using PHP's `__get()` and `__isset()` magic methods. These methods allow you to use properties of an object that are not defined in the object itself.

Here's an example.

foreach ( $posts as $key => $post ) {
// This:
echo $post->key1;
// is the same as this:
echo get_post_meta( $post->ID, 'key1', true );
}

If you make a `print_r( $post )` or `var_dump( $post )`, you will not see the "key1" property of the `$post` object. But the function [__get()](

[To see links please register here]

) allows you to access that property.

===========================================================

You have two general options in my opinion - loop through the posts and get the data that you need, like so(this code will go right after `$posts = query_posts( $args );`):

foreach ( $posts as $key => $post ) {
$posts[ $key ]->key1 = get_post_meta( $post->ID, 'key1', true );
$posts[ $key ]->key2 = get_post_meta( $post->ID, 'key2', true );
}

Or hook to the `the_posts` filter hook and do the same thing there(more work, but if you have multiple functions that need to add that data to each post - it might be easier). This code would go to your functions.php or your plugin's files(if you're making a plugin):

function my_the_posts_filter( $posts ) {
foreach ( $posts as $key => $post ) {
$posts[ $key ]->key1 = get_post_meta( $post->ID, 'key1', true );
$posts[ $key ]->key2 = get_post_meta( $post->ID, 'key2', true );
}

return $posts;
}

And then you would change your

$posts=query_posts( $args);

line to this:

add_filter( 'the_posts', 'my_the_posts_filter', 10 );

$posts = query_posts( $args );

remove_filter( 'the_posts', 'my_the_posts_filter', 10 );

Considering the fact that this would happen inside of an AJAX request, you can technically get rid of the `remove_filter()` call, but it's good to have it just in case you're going to make any other post queries in your code.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through