WordPress:如何使用query_posts返回元数据?

时间:2022-12-14 16:37:52

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.

我正在使用admin-ajax.php做一个AJAX请求,我根据检查的复选框过滤帖子。它工作得很好,虽然我很难找到一种方法来返回每个帖子的元细节。

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

我只是使用query_posts来获取我的数据,如下所示:

    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.

我如何修改它以返回元数据?我知道我可以使用meta_query按元数据过滤帖子,但我只想在帖子中显示数据。

1 个解决方案

#1


8  

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.

除了下面概述的解决方案,如果你使用WordPress> = 3.5(你应该:),你可以简单地使用WP_Post对象的魔术方法。

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.

基本上WP_Post对象(来自WP_Query的几乎所有查询结果中的posts数组)都使用PHP的__get()和__isset()魔术方法。这些方法允许您使用对象本身未定义的对象的属性。

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() allows you to access that property.

如果你创建了print_r($ post)或var_dump($ post),你将看不到$ post对象的“key1”属性。但函数__get()允许您访问该属性。

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

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 );):

在我看来,你有两个一般的选择 - 循环发布帖子并获取你需要的数据,就像这样(这个代码将在$ 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):

或者挂钩到the_posts过滤器钩子并在那里做同样的事情(更多的工作,但如果你有多个功能需要将这些数据添加到每个帖子 - 它可能更容易)。这段代码将转到你的functions.php或你的插件的文件(如果你正在制作一个插件):

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.

考虑到这可能发生在AJAX请求中,你可以在技术上摆脱remove_filter()调用,但是如果你要在你的代码中进行任何其他的帖子查询,那么它很好。

#1


8  

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.

除了下面概述的解决方案,如果你使用WordPress> = 3.5(你应该:),你可以简单地使用WP_Post对象的魔术方法。

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.

基本上WP_Post对象(来自WP_Query的几乎所有查询结果中的posts数组)都使用PHP的__get()和__isset()魔术方法。这些方法允许您使用对象本身未定义的对象的属性。

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() allows you to access that property.

如果你创建了print_r($ post)或var_dump($ post),你将看不到$ post对象的“key1”属性。但函数__get()允许您访问该属性。

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

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 );):

在我看来,你有两个一般的选择 - 循环发布帖子并获取你需要的数据,就像这样(这个代码将在$ 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):

或者挂钩到the_posts过滤器钩子并在那里做同样的事情(更多的工作,但如果你有多个功能需要将这些数据添加到每个帖子 - 它可能更容易)。这段代码将转到你的functions.php或你的插件的文件(如果你正在制作一个插件):

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.

考虑到这可能发生在AJAX请求中,你可以在技术上摆脱remove_filter()调用,但是如果你要在你的代码中进行任何其他的帖子查询,那么它很好。