WordPress教程

WordPress 实现通过自定义字段查询和排序

阿里云

WordPress 建网站时,可以根据指定自定义字段进行排序,也可以按照指定字段查询需要的类型。这些功能都是通过 WP_Query()方法来实现的,下面分享一下二种代码。

WordPress 通过自定义字段进行排序

也想出现在这里?联系我们
创客主机
  1. <?php
  2. $args = array(
  3. 'post_type' => 'product',//文章类型,可删除
  4. 'orderby' => array(
  5. 'meta_value_num'=>'ASC'
  6. ),
  7. 'meta_key' => 'sortnum',//sortnum是字段名
  8. );
  9. $query = new WP_Query( $args );
  10. while ($query->have_posts()) : $query->the_post(); ?>
  11. <li> <a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>
  12. <?php endwhile; ?>
  13. <?php wp_reset_query();?>

还可以这样写:

  1. <?php
  2. $args=array(
  3. 'meta_key' => 'views',//字段名
  4. 'orderby' => 'meta_value_num',//按字段值排序
  5. 'post__not_in' => get_option( 'sticky_posts' ),//排除置顶文章
  6. 'category__not_in' => array(1,2),//排序指定分类数组
  7. 'posts_per_page'=>8,//显示文章数量
  8. 'order' => 'DESC'
  9. );
  10. query_posts($args); while (have_posts()) : the_post();?>
  11. <li> <a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>
  12. <?php endwhile;wp_reset_query();?>

WordPress 通过自定义字段进行查询

  1. <?php
  2. $args = array(
  3. 'meta_query'=>array(
  4. array(
  5. 'key'=>'disabled',
  6. 'value'=>1,
  7. 'compare'=>'='
  8. )
  9. ),
  10. 'showposts' =>6,
  11. );
  12. $query = new WP_Query( $args );
  13. while ($query->have_posts()) : $query->the_post(); ?>
  14. <li> <a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>
  15. <?php endwhile; ?>
  16. <?php wp_reset_query();?>

也可以二者结合在一起实现查询和排序。

  1. <?php
  2. $args = array(
  3. 'post_type' => 'product',//文章类型
  4. 'orderby' => array(
  5. 'meta_value_num'=>'ASC'
  6. ),
  7. 'meta_key' => 'sort',//排序字段
  8. 'meta_query'=>array(
  9. array(
  10. 'key'=>'disabled',//查询字段
  11. 'value'=>1,
  12. 'compare'=>'='
  13. )
  14. ),
  15. 'showposts' =>6,
  16. );
  17. $query = new WP_Query( $args );
  18. while ($query->have_posts()) : $query->the_post(); ?>
  19. <li> <a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>
  20. <?php endwhile; ?>
  21. <?php wp_reset_query();?>

如果想多个条件筛选,可以在代码里多加 array,如下:

  1. <?php
  2. $args = array(
  3.  
  4. 'orderby' => array(
  5. 'meta_value_num'=>'ASC'
  6. ),
  7. 'meta_key' => 'paixu',//排序字段
  8.  
  9. 'meta_query'=>array(
  10. array(
  11. 'key'=>'paixu',//筛选字段1
  12. 'value'=>'',
  13. 'compare'=>'!='//不为空
  14. ),
  15. array(
  16. 'key'=>'演示网站',//筛选字段2
  17. 'value'=>'dedeym',
  18. 'compare'=>'LIKE'
  19. )
  20. ),
  21. 'showposts' =>300,//显示数量
  22. );
  23. $query = new WP_Query( $args );
  24. while ($query->have_posts()) : $query->the_post(); ?>
  25.  
  26. <li> <a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>
  27.  
  28. <?php endwhile; ?>
  29. <?php wp_reset_query();?>

WordPress 实现通过自定义字段查询和排序

已有 119 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!