最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Query Posts or Get Posts by custom fields, possible?

programmeradmin6浏览0评论

If I were to take a standard query post.

<?php query_posts('post_type=payment'); while (have_posts()) : the_post();?>

Only this time I would like to query the post by 2 custom fields that it may contain.

<?php query_posts('post_type=payment'.get_post_meta($post->ID,'bookingref', true).get_post_meta($post->ID,'customerref', true) ); while (have_posts()) : the_post(); ?>

That doesn't work. Is something like this possible and how is it done?

Any ideas?

Marvellous

If I were to take a standard query post.

<?php query_posts('post_type=payment'); while (have_posts()) : the_post();?>

Only this time I would like to query the post by 2 custom fields that it may contain.

<?php query_posts('post_type=payment'.get_post_meta($post->ID,'bookingref', true).get_post_meta($post->ID,'customerref', true) ); while (have_posts()) : the_post(); ?>

That doesn't work. Is something like this possible and how is it done?

Any ideas?

Marvellous

Share Improve this question asked Mar 3, 2011 at 17:31 Robin I KnightRobin I Knight 1,5517 gold badges21 silver badges28 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 19

To query posts by custom fields you can use the 'meta_query' parameter

<?php
$args = array(
    'post_type' => 'payment',
    'meta_query' => array(
            array(
                'key' => 'bookingref',
                'value' => 'the_value_you_want',
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'customerref',
                'value' => 'the_value_you_want',
                'compare' => 'LIKE'
            )
    )
);
query_posts($args); while (have_posts()) : the_post();
?>

you can't use get_post_meta inside the query because it gets you the value and not the key and also it accepts a Post ID to get that value of and before the query $post->id is not in the scope.

发布评论

评论列表(0)

  1. 暂无评论