Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress-将两个复杂查询合并为一个查询_Php_Wordpress - Fatal编程技术网

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

Php WordPress-将两个复杂查询合并为一个查询,php,wordpress,Php,Wordpress,我有两个相当复杂的WordPress查询,需要合并成一个查询 在我的第一个查询中,我得到了Wordpress发布日期的帖子。每天的帖子都包含在一个容器中,上面的日期作为标题。它也只隐藏任何过去的帖子。只显示今天或将来的帖子 在我的第二个查询中,我从一个自定义分类中获得WordPress帖子。对于分类法中的每个术语,post都显示在容器中,使用该术语的slug作为类,并显示术语的名称作为标题 我需要完成的事情和需要帮助的事情如下。 我需要和现在一样的第一个查询,但是,在第一个查询中,它说“//这里

我有两个相当复杂的WordPress查询,需要合并成一个查询

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

在我的第二个查询中,我从一个自定义分类中获得WordPress帖子。对于分类法中的每个术语,post都显示在容器中,使用该术语的slug作为类,并显示术语的名称作为标题

我需要完成的事情和需要帮助的事情如下。

我需要和现在一样的第一个查询,但是,在第一个查询中,它说“//这里需要代码以按分类术语显示帖子//”,我需要集成第二个查询,以便在当天输出帖子的地方,它也按第二个查询中的术语列出帖子

这两个查询本身都能很好地运行,但是我在实现一个查询时遇到了麻烦,这个查询利用了这两个查询的功能来完成我需要做的事情

以下是我的两个问题:

第一次查询:

<?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(); }  } ?>

第二次查询:

<?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(); }  } ?>
好的,开始吧(我没有像你的查询那样设置帖子,所以我没有测试它,但我相信这就是你要找的):

对于
$tax\u term->slug
$tax\u term->name
,您可以在查询循环中使用,如下所示:

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

echo $term->slug;
echo $term->name;
编辑 有关解释/澄清,请参见代码注释

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>';
}
if($query->have_posts()){
$\u tax=假;
$same_date=array();//相同日期的集合
$same\u date\u bool=true;//确定是否存在上一个发布日期
回声';
而($query->have_posts()){
$query->the_post();
$date=获取日期('l jS F Y');
$same_date_bool=true;
//检查是否从当前日期开始显示post
//那么,这篇文章应该被附加到上一篇文章的内部
//打开的div.day标签
if(!empty($same_date)&&in_数组($date,$same_date)){
$same\u date\u bool=false;
}
//收集所有日期
$same_date[]=$date;
//如果邮件不在同一日期
//创建新的div元素
如果($same_date_bool){
回声'
';
echo$日期;
回声';
}
//如果邮件不在同一日期
//创建新的div元素
如果($same_date_bool){
回声';
}
回声';
_标题(“,”);
回声';
$terms=获取条款($post->ID,$tax);
//注:我假设你的帖子会
//总是只有一个学期
$terms=array\u slice($terms,0);
$term=$terms[0];
如果($期限){
$\u tax=真;
回声';
回显“.$term->name.”;
}
_内容();
if(美元税){
回声';
}
回声';
//如果邮件在同一日期
//为上一个“div.day”元素追加结束div元素
如果(!$same\u date\u bool){
回声';
}
}
回声';
}

希望这有帮助。此外,我还将
调用者获取帖子
替换为
忽略粘性帖子
,因为
调用者获取帖子
自v3.1以来一直被弃用,您的查询中有两个不同的
order
/
orderby
值。Wordpress不支持订单值数组,因此无法合并。在这种情况下,您需要自定义SQL查询。第二个查询中的订单值并不重要。你能列出你想要的帖子的参数吗?我在第二个查询中看到,您正试图用headins按分类法进行组织?如上所述,我需要像在第一个查询中一样,按天返回帖子,每天的帖子都放在自己的容器中。在每天帖子的容器中,每个分类术语都应该循环使用,并显示税务术语的标题以及该税务术语中的任何帖子。下面是一个非常粗糙的流程图,它可能有助于澄清问题:你能举一个我将如何在这里输出帖子的示例吗?您的代码结构看起来可以满足我的需要,但是,我不确定如何使用此代码以我需要的方式输出帖子。@Zatechnodemous为了获得正确的结果,如果可能,请导出您的帖子数据,并在您的问题中添加链接,以便我们可以自己测试。谢谢您的详细介绍。我现在正在站点上测试代码,并获得数据输出,但是,这并不完全正确。我首先使用了提供的代码,现在在其中添加了一些额外的div来显示应该在哪里。首先,我拍摄了输出代码的屏幕截图,并添加了一些注释以突出问题。这可以在这里看到:-第二,这是我的页面中当前使用的代码,添加了新的div等等:-我试图更正自己,但没有运气。我有点力不从心。我自己一直在尝试解决这个问题,但我只是越来越多地打破它>\u@zatechnodemous如果你需要黄金代码,你应该提供示例数据。截图帮不了什么忙。