dusty khan
Forum Replies Created
-
Forum: Hacks
In reply to: Custom meta date order wont show two posts with same date.After some Googleing and further trawling through the interweb, I feel I am nearing the solution.
Thanks for all your help bcworkz It was greatly appreciated!
Here is where I am at:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $ordered_posts = new WP_Query(array( 'paged' => $paged, 'category_name' =>'big-lottery', 'post_type' => 'post', 'posts_per_page' => 24, 'meta_key' => 'fund_dead', 'orderby' => 'meta_value_num', 'order' => 'ASC', )); $unordered_posts = new WP_Query(array( 'category_name' =>'big-lottery', 'post_type' => 'post', 'paged' => $paged, 'posts_per_page' => 24 - $ordered_posts->post_count, )); while ($ordered_posts->have_posts()) : $ordered_posts->the_post(); // loop 1 ?> <div class="divide"></div> <div class="event-main"> <a href="<?php the_permalink() ?>"><h1><?php the_title(); ?></h1></a> <?php $eventDate = get_post_meta($post->ID, 'fund_dead', true); $today = esc_html_e( date( '' , strtotime( $fund_dead ) ) ); if($eventDate == $today) { ?> <h3>Deadline: <span id="event-list"><?php $date = 'Ongoing' ; echo($date); ?></span> </h3> <?php } else { ?> <h3>Deadline: <span id="event-list"><?php $fund_dead = get_post_meta($post->ID, 'fund_dead', true); esc_html_e( date_i18n( 'l, F jS, Y' , strtotime( $fund_dead ) ) ); ?></span> </h3> <?php } ?> <h3>Eligibility: <span id="event-list"><?php $fund_elig = get_post_meta($post->ID, 'fund_elig', true); echo apply_filters('', $fund_elig); ?></span> </h3> <a class="event-more" href="<?php the_permalink() ?>">Read More</a> </div> <div style="clear:both;"></div> <?php endwhile;?> <?php $my_query = null; $my_query = new WP_Query($unordered_posts); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); $custom = get_post_meta($my_query->post->ID, 'fund_dead', true); if ( ! $custom ){ ?> <div class="divide"></div> <div class="event-main"> <a href="<?php the_permalink() ?>"><h1><?php the_title(); ?></h1></a> <?php $eventDate = get_post_meta($post->ID, 'fund_dead', true); $today = esc_html_e( date( '' , strtotime( $fund_dead ) ) ); if($eventDate == $today) { ?> <h3>Deadline: <span id="event-list"><?php $date = 'Ongoing' ; echo($date); ?></span> </h3> <?php } else { ?> <h3>Deadline: <span id="event-list"><?php $fund_dead = get_post_meta($post->ID, 'fund_dead', true); esc_html_e( date_i18n( 'l, F jS, Y' , strtotime( $fund_dead ) ) ); ?></span> </h3> <?php } ?> <h3>Eligibility: <span id="event-list"><?php $fund_elig = get_post_meta($post->ID, 'fund_elig', true); echo apply_filters('', $fund_elig); ?></span> </h3> <a class="event-more" href="<?php the_permalink() ?>">Read More</a> </div> <div style="clear:both;"></div> <?php } endwhile; } wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Using the two query approach gets all the desired posts and lists them in the correct order. Phew! The only issue now is the that it is still ordering by year then month. This is good enough for the meantime, but I will still be searching for a fix to the year order issue.
Forum: Hacks
In reply to: Custom meta date order wont show two posts with same date.Thanks for your continued help ??
I have had to change what you supplied to:
<?php add_filter('posts_request', 'edit_posts_request' ); function edit_posts_request($request) { if(is_page('blog')) { $request['category_name'] = 'Blog'; $request[0] = "SELECT wp_ffaposts.* FROM wp_ffaposts INNER JOIN wp_ffaterm_relationships ON (wp_ffaposts.ID = wp_ffaterm_relationships.object_id) INNER JOIN wp_ffapostmeta ON (wp_ffaposts.ID = wp_ffapostmeta.post_id) INNER JOIN wp_ffapostmeta AS mt1 ON (wp_ffaposts.ID = mt1.post_id) WHERE 1=1 AND ( wp_ffaterm_relationships.term_taxonomy_id IN (9) ) AND wp_ffaposts.post_type = 'post' AND (wp_ffaposts.post_status = 'publish' OR wp_ffaposts.post_status = 'private') GROUP BY wp_ffaposts.ID ORDER BY STR_TO_DATE(wp_ffapostmeta.meta_value, %M %D, %Y) ASC"; } return $request; } $loop = new WP_Query( $request ); while ( $loop->have_posts() ) : $loop->the_post(); ?>
As
if( 'big-lottery' == $request[1]->get('category_name') )
was returning and error.It doesn’t seem to order the posts by date still. Very frustrating…
Forum: Hacks
In reply to: Custom meta date order wont show two posts with same date.I think I’m now even more lost :S
So is this what I need:
<?php add_filter('posts_orderby', 'edit_posts_orderby' ); function edit_posts_orderby($orderby) { $orderby = " STR_TO_DATE(wp_ffapostmeta.meta_value, %M %D, %Y) ASC "; return $orderby; } ?> <?php $args = array( 'category_name' =>'big-lottery', 'post_type' => 'post', 'posts_per_page' => -1, 'meta_key' => 'fund_dead', 'orderby' => 'meta_value', 'order'=>'ASC', 'meta_query'=> array( array( 'key' => 'fund_dead' , 'compare' => 'EXISTS', 'type' => 'DATE', array( 'key' => 'fund_dead' , 'compare' => 'NOT EXISTS', 'type' => 'DATE' ) ) ) );
Because this doesn’t return any posts at all…
How would I go about using you SQL Query to pull in the posts?
Forum: Hacks
In reply to: Custom meta date order wont show two posts with same date.Ok your query also only seems to show the posts with the date meta input, but again order correctly by day/month, but not year, so still having an issue pulling posts with empty fund_dead meta.
You can view the site HERE
Could you explain the ‘posts_orderby’ slightly better then the wp codex?
This is the SQL Query that I get returned with the ‘posts_request’ filter:
SELECT wp_ffaposts.* FROM wp_ffaposts INNER JOIN wp_ffaterm_relationships ON (wp_ffaposts.ID = wp_ffaterm_relationships.object_id) INNER JOIN wp_ffapostmeta ON (wp_ffaposts.ID = wp_ffapostmeta.post_id) INNER JOIN wp_ffapostmeta AS mt1 ON (wp_ffaposts.ID = mt1.post_id) WHERE 1=1 AND ( wp_ffaterm_relationships.term_taxonomy_id IN (9) ) AND wp_ffaposts.post_type = 'post' AND (wp_ffaposts.post_status = 'publish' OR wp_ffaposts.post_status = 'private') AND (wp_ffapostmeta.meta_key = 'fund_dead' AND mt1.meta_key = 'fund_dead' ) GROUP BY wp_ffaposts.ID ORDER BY wp_ffapostmeta.meta_value ASC
Forum: Hacks
In reply to: Custom meta date order wont show two posts with same date.Ok if I query like this it get all posts but My order is completely messed up… I am lost with the SQL… any thoughts?
<?php ; } elseif ( is_page( 'Big Lottery' ) || '172' == $post->post_parent ) { ?> <?php $args = array( 'category_name' =>'big-lottery', 'post_type' => 'post', 'posts_per_page' => -1, 'orderby' => 'meta_value', 'order'=>'ASC', 'meta_query'=> array( array( 'category_name' =>'big-lottery', 'post_type' => 'post', 'posts_per_page' => -1, 'meta_key' => 'fund_dead' , 'orderby' => 'meta_value', 'order'=>'ASC', ) ) ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); ?> <div class="divide"></div> <div class="event-main"> <a href="<?php the_permalink() ?>"><h1><?php the_title(); ?></h1></a> <?php $eventDate = get_post_meta($post->ID, 'fund_dead', true); $today = esc_html_e( date( '' , strtotime( $fund_dead ) ) ); if($eventDate == $today) { ?> <h3>Deadline: <span id="event-list"><?php $date = 'Ongoing' ; echo($date); ?></span> </h3> <?php } else { ?> <h3>Deadline: <span id="event-list"><?php $fund_dead = get_post_meta($post->ID, 'fund_dead', true); esc_html_e( date_i18n( 'l, F jS, Y' , strtotime( $fund_dead ) ) ); ?></span> </h3> <?php } ?> <h3>Eligibility: <span id="event-list"><?php $fund_elig = get_post_meta($post->ID, 'fund_elig', true); echo apply_filters('', $fund_elig); ?></span> </h3> <a class="event-more" href="<?php the_permalink() ?>">Read More</a> </div> <div style="clear:both;"></div> <?php endwhile;?>
Forum: Hacks
In reply to: Custom meta date order wont show two posts with same date.Thanks for your help bcworkz, I have gone for a much more simple approach, but now it doesn’t take the year into consideration and wont get posts where a custom date isn’t set.
(https://development.hanselldesign.co.uk/ffa/?page_id=172)
<?php $args = array( 'category_name' =>'big-lottery', 'post_type' => 'post', 'orderby' => 'meta_value', 'meta_key' => 'fund_dead', 'order'=>'ASC' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); ?> <div class="divide"></div> <div class="event-main"> <a href="<?php the_permalink() ?>"><h1><?php the_title(); ?></h1></a> <h3>Deadline: <span id="event-list"><?php $fund_dead = get_post_meta($post->ID, 'fund_dead', true); esc_html_e( date_i18n( 'l, F jS, Y' , strtotime( $fund_dead ) ) ); ?></span> </h3> <h3>Eligibility: <span id="event-list"><?php $fund_elig = get_post_meta($post->ID, 'fund_elig', true); echo apply_filters('', $fund_elig); ?></span> </h3> <a class="event-more" href="<?php the_permalink() ?>">Read More</a> </div> <div style="clear:both;"></div> <?php endwhile;?> <div class="divide"></div>