WordPress - 将两个复杂查询合并为一个

时间:2014-06-19 15:36:57

标签: php wordpress

我有两个相当复杂的WordPress查询,我需要将它们组合成一个查询。

在我的第一个查询中,我发布Wordpress帖子的日期。帖子的每一天都包含在一个容器中,日期显示在帖子上方作为标题。它也只隐藏过去的任何帖子。仅显示今天或未来的帖子。

在我的第二个查询中,我从自定义分类中获取WordPress帖子。对于分类中的每个术语,帖子显示在容器内,利用该术语的段作为类,并将术语的名称显示为标题。

我需要完成的工作以及我需要帮助的内容如下:

我需要完全像现在这样的第一个查询,但是,在第一个查询中,它说" //这里需要代码按分类词来显示帖子//",我需要整合第二个查询,以便当天输出帖子,它也按照第二个查询中的术语列出帖子。

这两个查询都可以完全独立运行,但我在实现单个查询时遇到问题,该查询利用两个查询的功能来完成我需要做的事情。

以下是我的两个问题:

第一次查询:

<?php 
// First we get yesterdays date
$year = date("Y"); $month = date("m"); $yesterday = date("d", strtotime("-1 day"));
// Then we construct a query to get items from the calendar, either published or scheduled for a future date so that they appear on the calendar. We use date query to hide events that have already passed by querying after the date we got before. We use yesterdays date to ensure that TODAYS post are still obtained
$query = new WP_Query(array('post_type' => 'calendar',
    'post_status' => 'publish,future',
    'orderby' => 'post_date',
    'order' => 'DESC',
    'date_query' => array(
        array('after' => array(
                'year'  => $year,
                'month' => $month,
                'day'   => $yesterday
            )
        )
     )
 )); 
// This is a special loop that gets posts by day and encases each days worth of posts in a container and shows the date of those posts. jQuery will be applied to this
if ($query->have_posts()) {
    echo '<div class="day-posts">';
    while ($query->have_posts()) {
        $query->the_post();
        echo '<div class="day">';
        the_date('l jS F Y', '<div class="title"><div>', '</div>'); //Formats date, before echo, after echo
        echo '<div class="posts clearfix">';
        echo '<div class="post">';
        the_title('<div class="title">', '</div>');
        echo '<div class="content">';
        // Need custom code here to display posts by taxonomy //
        the_content();
        echo '</div></div></div></div>';
    }
    echo '</div>';
}?>

第二次查询:

<?php
$post_type = array('calendar');
$tax = 'event-category';
$tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC'));
if ($tax_terms) {
    foreach ($tax_terms as $tax_term) {
        $args = array(
            'post_type' => $post_type,
            "$tax" => $tax_term->slug,
            'post_status' => 'publish',
            'posts_per_page' => - 1,
            'caller_get_posts' => 1
            ); // END $args
        $my_query = null;
        $my_query = new WP_Query($args);
        if ($my_query->have_posts()) {
            echo '<div class="' . $tax_term->slug . '">';
            echo '<div class="title">' . $tax_term->name . '</div>;
            while ($my_query->have_posts()) : $my_query->the_post();
?>

<?php the_title();?>
<?php the_content();?>

<?php endwhile; echo '</div>'; } wp_reset_query(); }  } ?>

我希望我已经很好地解释了我想要做的事情。如果我没有,或有问题,请询问,我会尽快回答。

1 个答案:

答案 0 :(得分:2)

好的,你去(我没有像你的查询设置的帖子,所以我没有测试它,但我相信你正在寻找的):

// Setup the basic structure:
$args = array(
    'post_type' => 'calendar',
    'post_status' => 'publish,future',
    'orderby' => 'post_date',
    'order' => 'DESC',
    'date_query' => array(
        array(
            'after' => array(
                'year'  => $year,
                'month' => $month,
                'day'   => $yesterday
            )
        )
    ),
    'posts_per_page' => -1,
    'caller_get_posts' => 1
);

// For dynamic taxonomy term create a bootstrap tax_query

$tax = 'event-category';
$tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC'));
if ($tax_terms) {

    $args['tax_query'] = array(
        array(
            'taxonomy' => $tax,
            'field' => 'slug',
        )
    );

    // Append the slugs to the tax_query terms
    foreach ($tax_terms as $tax_term) {
        $args['tax_query'][0]['terms'][] = $tax_term->slug;
    }
}

// Your One and only QUERY
$my_query = new WP_Query($args);

对于$tax_term->slug$tax_term->name,您可以在查询循环中使用get_the_terms(),如:

$terms = get_the_terms($post->ID, $tax);
$terms = array_slice($terms, 0);
$term = $terms[0];

echo $term->slug;
echo $term->name;

EDITED

请参阅代码注释以获得解释/说明

if ($query->have_posts()) {
    $_tax = false;
    $same_date = array(); // collection of same dates
    $same_date_bool = true; // determine previous post date exist

    echo '<div class="day-posts">';
    while ($query->have_posts()) {
        $query->the_post();
        $date = get_the_date('l jS F Y');

        $same_date_bool = true;

        // Checking if post is displayed from the current date
        // then this post should be appended inside to previous
        // opened div.day tag
        if (!empty($same_date) && in_array($date, $same_date)) {
            $same_date_bool = false;
        }

        // Collecting all dates
        $same_date[] = $date;

        // If post is not in same date
        // create new div element

        if ($same_date_bool) {
            echo '<div class="day">
                <div class="title">';
            echo $date;
            echo '</div>';
        }

        // If post is not in same date
        // create new div element

        if ($same_date_bool) {
            echo '<div class="posts clearfix">';
        }

        echo '<div class="post">';
        the_title('<div class="title">', '</div>');
        echo '<div class="content">';

        $terms = get_the_terms($post->ID, $tax);
        // Note: I'm assuming your post will
        // always have single term
        $terms = array_slice($terms, 0);
        $term = $terms[0];

        if ($term) {
            $_tax = true;
            echo '<div class="' . $term->slug . '">';
            echo '<div class="title">' . $term->name . '</div>';
        }

        the_content();

        if ($_tax) {
            echo '</div>';
        }

        echo '</div></div>';

        // If post is in same date
        // append closing div element for previous "div.day" element
        if (!$same_date_bool) {
            echo '</div></div>';
        }
    }

    echo '</div>';
}

希望现在有所帮助。此外,我已将caller_get_posts替换为ignore_sticky_posts,因为自{v3.1}以来已弃用caller_get_posts