I am trying to build a post filter on my website. The post page needs to display two similar posts in the footer. But they have to meet certain conditions.
Conditions:
-
First condition: A post has a number value(example:160) the two similar posts must be with in range of that value (example: if range is 10 a post with a 155 value would be in range of 160 ).
-
Second condition: The similar posts are of the same category as the main post
-
Third condition: If first two conditions fail, show predetermined post
Loops:
-
if the first condition yields two results the loop must stop.
-
if the first condition yields only one result the loop must continue with Second condition so two result get showen(the first post matching the first condition and the second post matching the second condition).
-
if the first condition loop yields no results use the second condition for both posts.
-
if the first and the second fail use the third.
Problems:
-The loop does not go trough all posts on the first condition it just goes through the first two lastest posts. How I can a make the conditions go through all posts first before continuing to the next condition?
Could someone explain to me how I can achieve my goal or atleast point me in the right direction. Thanks in advance
What I have got so far (width comments):
<?php
$orig_post = $post;
$orig_city = get_post_meta( $post->ID, 'property_city', true );
// The post range value for main post
$orig_area = (int)str_replace( array( ' ', ',', '.' ), '', get_post_meta( $post->ID, 'property_area', true ) );
// The post category for main post
$orig_category = wp_get_post_terms( $post->ID, 'property_category', array( 'fields' => 'ids' ) );
$exclude_ids = array( $post->ID );
// display all posts
$args = array(
'posts_per_page' => -1,
'post_type' => 'property',
'post_status' => 'publish',
'post__not_in' => $exclude_ids,
);
if ( $my_query->have_posts() && $orig_category ) {
// Counter to display only two posts
$counter = 0;
while ( $my_query->have_posts() and $counter < 1 ) {
$counter++;
$my_query->the_post();
$s_id = get_the_ID();
// The range
$s_area = (int)str_replace( array( ' ', ',', '.' ), '', get_post_meta( $s_id, 'property_area', true ) );
$min = $orig_area - 10;
$max = $orig_area + 10;
// The first condition
if ( ( $min <= $s_area ) and ( $s_area <= $max ) ) {
echo 'first condition met';
// The second condition
} elseif ( $orig_category ) {
echo 'second condition met';
// The third condition
} else {
echo 'third condition met';
}
?>
<?php } ?>
<?php }
$post = $orig_post;
wp_reset_query();
?>
Aucun commentaire:
Enregistrer un commentaire