我陷入了这个问题:我有一个自定义查询,在其中我不能按任何东西排序。我想按特定的meta\\u值排序。但当按姓名、头衔、日期等排序时,我仍然不能。我做错了什么?
<?php
// Get the parent cat thanks to the page\'s cat slug
$parent_cat = get_category_by_slug( $cat[1] );
$args = array(
\'type\' => \'post\',
\'child_of\' => $parent_cat->term_id
);
// child cats
$categories = get_categories( $args );
// loop on the child cats to get the sub cats object
if ($categories) {?>
<!-- THE TITLE -->
<div class="text-seperator">
<h2>Sector\'s programs</h2>
</div>
<ul class="sc_accordion">
<?php
foreach ($categories as $key => $value) {
$args = array(
\'post_type\' => \'editorial-posts\',
\'meta_key\' => \'meta_post_order\',
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => array( $value->slug ),
\'include_children\' => false
),
array(
\'taxonomy\' => \'post_tag\',
\'field\' => \'slug\',
\'terms\' => array( \'short-description\' )
)
)
);
$program_desc_short = new WP_Query( $args );
?>
<?php if ($program_desc_short->have_posts()) : $program_desc_short->the_post() ?>
<?php
//var_dump( get_post_meta( get_the_ID() ) );
$metas = get_post_meta( get_the_ID(), \'meta_post_order\' );
$order = $metas[0];
?>
<li><?php echo $order ?><a class="sc_accordion-btn" href="#"><?php echo $value->name ?></a>
<div class="sc_accordion-content"><?php the_content(); ?>
<?php edit_post_link(__(\'Edit This\'),\'<p class="edit-link">\' ,\'</p>\'); ?>
<a href="<?php echo \'/sectors/\'.$value->slug ?>" class="btn small-btn right">Read more</a>
</div>
<?php endif;
wp_reset_query();
}
?>
</ul>
<?php
}
?>
谢谢你的帮助
SO网友:Neovea
好吧,问题是因为我在类别上循环以获得匹配的帖子。我要做的是得到所需的类别,并在我的$args
+ 一些调整。
<?php
// Get the parent cat thanks to the page\'s cat slug
$parent_cat = get_category_by_slug( $cat[1] );
$cats_args = array(
\'type\' => \'post\',
\'child_of\' => $parent_cat->term_id
);
// child cats
$categories = get_categories( $cats_args );
// loop on the child cats to get the sub cats object
if ($categories) {
// empty array to store cats
$cats = array();
// Get the cat slugs and store it in an array
foreach ($categories as $key => $value)
$cats[] = $value->slug;
?>
<!-- THE TITLE -->
<div class="text-seperator">
<h2>Sector\'s programs</h2>
</div>
<ul class="sc_accordion">
<?php
$args = array(
\'post_type\' => \'editorial-posts\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => $cats,
\'include_children\' => false
),
array(
\'taxonomy\' => \'post_tag\',
\'field\' => \'slug\',
\'terms\' => array( \'short-description\' )
)
)
);
$program_desc_short = new WP_Query( $args );
?>
<?php if ($program_desc_short->have_posts()): while ($program_desc_short->have_posts()) : $program_desc_short->the_post() ?>
<?php
$terms = get_the_terms( get_the_ID(), \'category\' );
$attached_category = array();
foreach ($terms as $term)
// in order to only get the concerned category, we check that the term is matching the cats array
if (in_array($term->slug, $cats))
foreach ($term as $key => $value)
$attached_category[$key] = $value;
?>
<li><a class="sc_accordion-btn" href="#"><?php echo $attached_category[\'name\'] ?></a>
<div class="sc_accordion-content">
<?php the_content(); ?>
<?php edit_post_link(__(\'Edit This\'),\'<p class="edit-link">\' ,\'</p>\'); ?>
<a href="<?php echo \'/sectors/\'.$attached_category[\'slug\'] ?>" class="btn small-btn right">Read more</a>
</div>
<?php
endwhile;
endif;
wp_reset_query();
?>
</ul>
<?php
}
?>