Thursday, 15 August 2013

php - WordPress - Merging two complex queries into one -



php - WordPress - Merging two complex queries into one -

i have 2 complex wordpress queries need combine single query.

in first query, getting wordpress posts date published. each day of posts contained within container, date showed above posts title. hides posts in past. posts today or future shown.

in sec query, getting wordpress posts custom taxonomy. each term in taxonomy, posts displayed within container utilizing slug of term class, , displaying name of term title.

what need accomplish , need help with, follows.

i need first query now, however, in first query, says "// need code here display posts taxonomy term //", need integrate sec query posts beingness output day, listing posts term in sec query.

both queries function on own, having problem implementing single query utilizes functionality both queries need do.

here 2 queries:

1st query:

<?php // first yesterdays date $year = date("y"); $month = date("m"); $yesterday = date("d", strtotime("-1 day")); // build query items calendar, either published or scheduled future date appear on calendar. utilize date query hide events have passed querying after date got before. utilize yesterdays date ensure todays post 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 ) ) ) )); // special loop gets posts day , encases each days worth of posts in container , shows date of posts. jquery applied 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 display posts taxonomy // the_content(); echo '</div></div></div></div>'; } echo '</div>'; }?>

2nd query:

<?php $post_type = array('calendar'); $tax = 'event-category'; $tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'asc')); if ($tax_terms) { foreach ($tax_terms $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(); } } ?>

i hope have done job of explaining trying do. if have not, or there questions, please inquire , i'll reply quick can.

ok here go (i dont have posts setup query didn't test believe looking for):

// setup 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 ); // dynamic taxonomy term create 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 slugs tax_query terms foreach ($tax_terms $tax_term) { $args['tax_query'][0]['terms'][] = $tax_term->slug; } } // 1 , query $my_query = new wp_query($args);

for $tax_term->slug , $tax_term->name utilize get_the_terms() within query loop like:

$terms = get_the_terms($post->id, $tax); $terms = array_slice($terms, 0); $term = $terms[0]; echo $term->slug; echo $term->name; edited

see code comments explanation/clarification

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 displayed current date // post should appended within previous // opened div.day tag if (!empty($same_date) && in_array($date, $same_date)) { $same_date_bool = false; } // collecting dates $same_date[] = $date; // if post 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 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 post // 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 in same date // append closing div element previous "div.day" element if (!$same_date_bool) { echo '</div></div>'; } } echo '</div>'; }

hope helps now. have replaced caller_get_posts ignore_sticky_posts because caller_get_posts has been deprecated since v3.1

php wordpress

No comments:

Post a Comment