在我的搜索页面按日期排序wordpress。 但我想在我的模板中对meta_key进行排序 - imic_date(事件日期)。 我需要修改搜索查询,但在哪里? 应该工作的代码。但是......我在哪里添加它?
$args = array(
'post_type' => array('custom-post-type'),
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'imic_date'
);
$loop = new WP_Query($args);
(对不起我的英文):D
编辑:
我的search.php文件:
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-md-9 posts-archive">
<?php
if (have_posts()) :
while (have_posts()):the_post();
echo'<article class="post">
<div class="row">';
if (has_post_thumbnail()):
echo '<div class="col-md-4 col-sm-4">
<a href="' . get_permalink() . '">';
the_post_thumbnail('600x400', array('class' => "img-thumbnail"));
echo'</a></div>';
endif;
echo '<div class="col-md-8 col-sm-8">';
echo '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>';
echo '<span class="post-meta meta-data">
<span><i class="fa fa-calendar"></i>' . get_the_time(get_option('date_format')) . '</span><br><span>Time:<br/>Date:<br/>Price:<br/>Venue:<br/>Category:</span><span></i><br/>'.imic_custom_taxonomies_terms_links().'</span> <span>';
comments_popup_link('<i class="fa fa-comment"></i>'.__('No comments yet','framework'), '<i class="fa fa-comment"></i>1', '<i class="fa fa-comment"></i>%', 'comments-link',__(' ','framework'));
echo'</span></span>';
//echo imic_excerpt(50);
echo '<p><a href="' . get_permalink() . '" class="btn btn-primary">' . __('More ', 'framework') . '<i class="fa fa-long-arrow-right"></i></a></p>';
echo '</div></div>';
echo '</article>';
endwhile;
else:
echo '<article class="post">';
if (current_user_can('edit_posts')) :
?>
<h3><?php _e('NOT', 'framework'); ?></h3>
<p><?php printf(__('NOT', 'framework'), admin_url('post-new.php')); ?></p>
<?php else : ?>
<h3><?php _e('NOT', 'framework'); ?></h3>
<p><?php printf(__('NOT', 'framework')); ?></p>
<?php
echo '</article>';
endif;
?>
<?php
endif; // end have_posts() check
pagination();
?>
</div>
<!-- Start Sidebar -->
<?php
echo '<div class="col-md-3 sidebar">';
dynamic_sidebar('sidebar');
echo '</div>';
?>
<!-- End Sidebar -->
</div>
</div>
<?php get_footer(); ?>
答案 0 :(得分:2)
试试这段代码。
$args = array(
'post_type' => array('custom-post-type'),
'order_by' => array('imic_date'),
'order' => 'ASC',
);
$loop = new WP_Query($args);
编辑
像这样修改你的代码,
$args = array(
'post_type' => array('custom-post-type'),
'order_by' => array('imic_date'),
'order' => 'ASC',
);
$loop = new WP_Query($args);
if ($loop->have_posts()) :
while ($loop->have_posts()):$loop->the_post();
/** and then your code as used in original file */
编辑,
使用下面的代码,它可以工作(我测试过)
<?php $args = array(
'post_type' => 'event',
'orderby' => 'meta_value',
'meta_query' => array(array('key' => 'imic_date')),
'order' => 'desc',
'paged'=>$paged,
);
$loop = new WP_Query($args);
if ($loop->have_posts()) :
while ($loop->have_posts()):$loop->the_post(); ?>
<?php the_title(); ?>
以及此处的其余代码..
答案 1 :(得分:0)
全球$ wpdb;
$ tb_posts = $ wpdb-&gt;前缀。 &#39;帖子&#39 ;;
$ tb_postmeta = $ wpdb-&gt;前缀。 &#39; postmeta&#39 ;;
$ query = $ wpdb-&gt; get_results(&#34; SELECT $ colums 来自$ tb_posts INNER JOIN $ tb_postmeta ON $ tb_posts.ID = $ tb_postmeta.post_id WHERE $ tb_postmeta.meta_key IN(&#39; your_meta_key&#39;) AND $ tb_posts.post_status =&#39;发布&#39; ORDER BY post_date DESC 限制1 &#34; );
return $query;