如何查询自定义帖子,然后按元值显示部分

时间:2012-11-21 作者:Chris

这似乎是一件容易的事情,但我在这里错过了一些东西。我有一个名为“收件人”的自定义帖子类型和一个名为“收件人”的元复选框yes_current“-(我们帮助患有癌症的儿童并为他们举办活动,因此我们需要知道谁是当前的接受者,谁是过去的接受者。)我们希望在收件人页面上显示两个部分:当前收件人和过去的收件人。

我的想法是查询循环以获取custom_post_types 该相等的收件人,然后在当前部分中显示所有当前收件人的特征图像(如果“yes\\u CURRENT”处于启用状态),然后在过去部分中对过去的收件人执行相同操作。

谢谢大家!

代码:

<h1>CURRENT Recipients</h1>

<?php $current_loop = new WP_Query(array(\'post_type\' => \'recipient\', \'posts_per_page\' => \'-1\', \'yes_current\' => \'on\'));

if( $current_loop->have_posts() ): while( $current_loop->have_posts() ): $current_loop-the_post(); 

<?php get_the_post_thumbnail($page->ID, \'thumbnail\'); ?>

<?php endwhile; endif; wp_reset_postdata(); ?>

<h1>PAST Recipients</h1>

<?php $current_loop = new WP_Query(array(\'post_type\' => \'recipient\', \'posts_per_page\' => \'-1\', \'yes_current\' => \'off\'));

if( $current_loop->have_posts() ): while( $current_loop->have_posts() ): $current_loop->the_post(); 

<?php get_the_post_thumbnail($page->ID, \'thumbnail\'); ?>

<?php endwhile; endif; wp_reset_postdata(); ?>
编辑自定义post元代码:

$meta_boxes[] = array( \'id\' => \'yes_current\', \'title\' => \'Current Recipient\', \'pages\' => array( \'recipient\' ), \'context\' => \'side\', \'priority\' => \'low\', \'fields\' => array( array( \'name\' => \'Current Recipient\', \'id\' => "{$prefix}yes_current", \'type\' => \'checkbox\', // Value can be 0 or 1 \'std\' => 1, ), ),

2 个回复
最合适的回答,由SO网友:Chris 整理而成

所以我想出来了。因为我使用的是Deluxe Blog Tips上的Meta框,所以我使用了他们的helper函数来代替get\\u post\\u Meta。以下是显示“我的自定义\\u post\\u类型收件人”的当前收件人列表和过去收件人列表的最后一段代码。要与值进行比较的元框为rec\\u yes\\u current:

<h1>Current Recipients</h1>
<?php /* Start loop */ ?>
<?php $linkposts = new WP_Query(array(\'post_type\' => \'recipient\', \'posts_per_page\' => \'-1\'));
  while ($linkposts->have_posts()) : $linkposts->the_post(); 
  $linkvalue = $linkposts->ID;
    $image = wp_get_attachment_image_src( get_post_thumbnail_id($linkvalue), \'post-thumbnail\' );
    $medical = get_post_meta(get_the_id(), \'rec_medical\', true);
$twitter = get_post_meta(get_the_id(), \'rec_twitter\', true);
$facebook = get_post_meta(get_the_id(), \'rec_facebook\', true);
$dob = get_post_meta(get_the_id(), \'rec_dob\', true);
    $today = date("m/d/Y");
    list($m,$d,$y) = explode(\'/\',$dob);
    list($tm,$td,$ty) = explode(\'/\',$today);
$year=$ty-$y;
    $current = rwmb_meta( \'rec_yes_current\' ); 
?>

<?php  // if is current
if($current == \'\') {
echo \'<div class="single recipient">\';
echo \'<p class="recname"><a href="\' . get_permalink($linkvalue) . \'">\' . get_the_title($linkvalue) . \'</a></p>\';
echo \'<a href="\' . get_permalink($linkvalue) . \'" class="frame">\' . get_the_post_thumbnail($linkvalue, \'thumbnail\') . \'</a>\';
echo \'<p class="year"><strong>Age</strong>: \' . $year . \' years old</p>\';
echo \'<p class="condition"><strong>Conditions</strong>:\' . $medical . \'</p>\';       
echo \'<p class="readmore"><a href="\' . get_permalink($linkvalue) . \'">read more</a></p>\';
echo \'</div>\';
}; ?>

<h1>Past Recipients</h1>

<?php  // if is current
if($current != \'\') {
    echo \'<div class="single recipient">\';
    echo \'<p class="recname"><a href="\' . get_permalink($linkvalue) . \'">\' . get_the_title($linkvalue) . \'</a></p>\';
    echo \'<a href="\' . get_permalink($linkvalue) . \'" class="frame">\' . get_the_post_thumbnail($linkvalue, \'thumbnail\') . \'</a>\';
echo \'<p class="year"><strong>Age</strong>: \' . $year . \' years old</p>\';
echo \'<p class="condition"><strong>Conditions</strong>:\' . $medical . \'</p>\';       
echo \'<p class="readmore"><a href="\' . get_permalink($linkvalue) . \'">read more</a></p>\';
echo \'</div>\';
}; ?>

<?php endwhile; wp_reset_query(); ?>
如果有更干净/更好的方法来写这篇文章,我很乐意看到。谢谢大家!

SO网友:Chip Bennett

我建议执行两个单独的查询,通过meta_query 针对您的帖子自定义元键值。

$current_query_args = array(
    \'post_type\' => \'recipient\',
    \'meta_query\' => array(
        array(
            \'key\' => \'yes_current\',
            \'value\' => \'on\',
            \'compare\' => \'=\'
        )
    )
);

$current_query = new WP_Query( $current_query_args );

if ( $current_query->have_posts() ) : while ( $current_query->have_posts() ) : $current_query->the_post();

    // Loop goes here

endwhile; endif;

wp_reset_postdata();

$past_query_args = array(
    \'post_type\' => \'recipient\',
    \'meta_query\' => array(
        array(
            \'key\' => \'yes_current\',
            \'value\' => \'off\',
            \'compare\' => \'=\'
        )
    )
);

$past_query = new WP_Query( $past_query_args );

if ( $past_query->have_posts() ) : while ( $past_query->have_posts() ) : $past_query->the_post();

    // Loop goes here

endwhile; endif;

wp_reset_postdata();

结束

相关推荐

Metaboxes not saving data

我正在为一个插件重建一个元盒,它现在不会保存我输入的数据。HTML几乎与以前完全相同,但save\\u元函数根本不起作用。我已经阅读了不少教程,在StackExchange和其他几个网站上查看了大量问题,并试图找出任何容易的陷阱,但这些似乎都不是问题所在。有人能看看这个,告诉我我错过了什么吗?备注:这都是一节课。time\\U date\\U元函数中的数组用于另一个返回HTML的函数。据我所知,这不是问题所在。HTML字段显示正确,其源代码与我之前手动写出的HTML字段几乎相同。手写HTML与此函数输出之