admin管理员组文章数量:1405170
I have an old project (custom post type query) that needs an alteration and my memory/skill level is lacking for what to do. With the code below, I'm needing to return the first query in alphabetical order, by title. Using orderby => title
and order => asc
(line 9 and 10) has no effect. It works in the second query (lines 39 & 40), though. I had a colleague help me set this up originally but he is not available to help with this tweak. Any help by the SE community would be appreciated.
<?php get_header(); ?>
<h1>Properties in <?php the_terms('', 'region');?></h1>
<?php
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$args = array(
'post_type' => 'rental',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'region',
'field' => 'slug',
'terms' => ".$queried_object->slug."
)
)
);
$query = get_posts( $args );
$property=array();
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $term_list = wp_get_post_terms($post->ID, 'property', array("fields" => "ids"));
foreach($term_list as $id)
{
array_push($property,$id);
}
?>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
<?php wp_reset_query();
$property=array_unique($property);
foreach($property as $ids)
{$terms=get_term_by('id', $ids, 'property');
?><h1><?php echo $terms->name ?></h1><?php
?>
<?php
$args1 = array(
'post_type' => 'rental',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'property',
'field' => 'id',
'terms' => $ids
),
array('taxonomy' => 'region',
'field' => 'slug',
'terms' => ".$queried_object->slug.")
)
);
$query1 = new WP_Query( $args1 );
foreach($query1->posts as $posts)
{
$region_array=get_the_terms($posts->ID,'region');
$property_array = get_the_terms( $posts->ID, 'property' );
$bedrooms_array = get_the_terms( $posts->ID, 'bedrooms' );
$bathrooms = get_the_terms( $posts->ID, 'bathrooms' );
$views = get_the_terms( $posts->ID, 'view' );
$rows = get_field('rental_images',$posts->ID); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['rental_images-image' ]; // get the sub field value
$image = wp_get_attachment_image_src( $first_row_image, 'thumbnail' );
?>
<div class="rental">
<div class="pic">
<a href="<?php echo get_permalink($posts->ID);?>"><img src="<?php echo $image[0]; ?>" /></a>
</div><!-- .pic -->
<div class="text">
<ul>
<li><?php echo $posts->post_title ?></li>
<?php
if($bedrooms_array!='' )
{$i=0;
?><li>Bedrooms:<?php
foreach($bedrooms_array as $bed)
{
if($i==0)
{
$i=1;
?>
<?php echo $bed->name; ?>
<?php }
else
{
echo ", ".$bed->name;
}
}
?></li><?php
}
else
{
}?>
<?php
if($bathrooms!='')
{?><li>Bathrooms: <?php
$i=0;
foreach($bathrooms as $bath)
{
if($i==0)
{
echo $bath->name;
$i=1;
}
else
{
echo ", ".$bath->name;
}}?></li><?php
}?>
<?php
if($views!='')
{?><li>View: <?php
$i=0;
foreach($views as $v)
{
if($i==0)
{
$i=1;
echo $v->name;
}
else
{
echo ", ".$v->name;
}
}?></li><?php }?>
</ul>
</div><!-- .text -->
</div><!-- .rental -->
<?php }
}?>
<?php get_footer(); ?>
I have an old project (custom post type query) that needs an alteration and my memory/skill level is lacking for what to do. With the code below, I'm needing to return the first query in alphabetical order, by title. Using orderby => title
and order => asc
(line 9 and 10) has no effect. It works in the second query (lines 39 & 40), though. I had a colleague help me set this up originally but he is not available to help with this tweak. Any help by the SE community would be appreciated.
<?php get_header(); ?>
<h1>Properties in <?php the_terms('', 'region');?></h1>
<?php
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$args = array(
'post_type' => 'rental',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'region',
'field' => 'slug',
'terms' => ".$queried_object->slug."
)
)
);
$query = get_posts( $args );
$property=array();
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $term_list = wp_get_post_terms($post->ID, 'property', array("fields" => "ids"));
foreach($term_list as $id)
{
array_push($property,$id);
}
?>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
<?php wp_reset_query();
$property=array_unique($property);
foreach($property as $ids)
{$terms=get_term_by('id', $ids, 'property');
?><h1><?php echo $terms->name ?></h1><?php
?>
<?php
$args1 = array(
'post_type' => 'rental',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'property',
'field' => 'id',
'terms' => $ids
),
array('taxonomy' => 'region',
'field' => 'slug',
'terms' => ".$queried_object->slug.")
)
);
$query1 = new WP_Query( $args1 );
foreach($query1->posts as $posts)
{
$region_array=get_the_terms($posts->ID,'region');
$property_array = get_the_terms( $posts->ID, 'property' );
$bedrooms_array = get_the_terms( $posts->ID, 'bedrooms' );
$bathrooms = get_the_terms( $posts->ID, 'bathrooms' );
$views = get_the_terms( $posts->ID, 'view' );
$rows = get_field('rental_images',$posts->ID); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['rental_images-image' ]; // get the sub field value
$image = wp_get_attachment_image_src( $first_row_image, 'thumbnail' );
?>
<div class="rental">
<div class="pic">
<a href="<?php echo get_permalink($posts->ID);?>"><img src="<?php echo $image[0]; ?>" /></a>
</div><!-- .pic -->
<div class="text">
<ul>
<li><?php echo $posts->post_title ?></li>
<?php
if($bedrooms_array!='' )
{$i=0;
?><li>Bedrooms:<?php
foreach($bedrooms_array as $bed)
{
if($i==0)
{
$i=1;
?>
<?php echo $bed->name; ?>
<?php }
else
{
echo ", ".$bed->name;
}
}
?></li><?php
}
else
{
}?>
<?php
if($bathrooms!='')
{?><li>Bathrooms: <?php
$i=0;
foreach($bathrooms as $bath)
{
if($i==0)
{
echo $bath->name;
$i=1;
}
else
{
echo ", ".$bath->name;
}}?></li><?php
}?>
<?php
if($views!='')
{?><li>View: <?php
$i=0;
foreach($views as $v)
{
if($i==0)
{
$i=1;
echo $v->name;
}
else
{
echo ", ".$v->name;
}
}?></li><?php }?>
</ul>
</div><!-- .text -->
</div><!-- .rental -->
<?php }
}?>
<?php get_footer(); ?>
Share
Improve this question
asked Oct 21, 2015 at 1:01
codeviewcodeview
4551 gold badge13 silver badges25 bronze badges
1 Answer
Reset to default 0I think you need to read up a bit on how The Loop works in Wordpress: https://codex.wordpress/The_Loop
And how the WP_Query class: https://codex.wordpress/Class_Reference/WP_Query
I am assuming your only want 2 loops, at the moment your have 3.
On line 19, which stores its posts in a var $query then does nothing.
On line 21, which is your main unaltered loop.
On line 54, which your colleague helps you with? Seems to be working.
So what you need to do is pass the arguments from your first loop to your second loop instead.
You can do this by making a new WP_Query like you did on line 53.
$query = get_posts( $args ); // line 19
becomes:
$query = new WP_Query( $args );
and Loop through the results using the have_posts() function.
if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); // line 21
本文标签: Help ordering custom query by TitleAscending
版权声明:本文标题:Help ordering custom query by Title, Ascending 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744883430a2630344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论