Query custom post type and custom field by URL parameters

I have a custom post type staff. and a custom field alphabet for posts in that post type. Each staff will be assigned to

I have a custom post type staff. and a custom field alphabet for posts in that post type. Each staff will be assigned to a letter either A or B ... Z based on the first letter of the staff's name.

On the directory page there will be navigation links A, B, ... Z. Is it possible to list all posts with in each letter via URL parameters?

Example click link A to list all staff with name begins with letter A (Adam, Andrew...):

/?post_type=staff&meta_key=alphabet&meta_value=A

I have a custom post type staff. and a custom field alphabet for posts in that post type. Each staff will be assigned to a letter either A or B ... Z based on the first letter of the staff's name.

On the directory page there will be navigation links A, B, ... Z. Is it possible to list all posts with in each letter via URL parameters?

Example click link A to list all staff with name begins with letter A (Adam, Andrew...):

http://example/?post_type=staff&meta_key=alphabet&meta_value=A
Share Improve this question edited Sep 6, 2019 at 3:50 Stickers asked Sep 6, 2019 at 3:45 StickersStickers 2521 gold badge6 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

To achieve this, you can start by implementing the solution for this question. That will allow you to filter posts by their first letter (no custom meta required):

Assuming starts_with is the name of the argument we want to use, we can filter posts_where to add a WHERE clause limiting results to those that begin with the given value if starts_with has been set on the query:

function wpse_298888_posts_where( $where, $query ) {
  global $wpdb;

  $starts_with = $query->get( 'starts_with' );

  if ( $starts_with ) {
      $where .= " AND $wpdb->posts.post_title LIKE '$starts_with%'";
  }

  return $where;
}
add_filter( 'posts_where', 'wpse_298888_posts_where', 10, 2 );

With this filter added, we can query posts like this:

$query = new WP_Query( array(
  'starts_with' => 'M',
) );

That will return all posts beginning with "M".

To support filtering the post type archive this way via the URL, you just need to register starts_with as a query var:

function wpse_346729_query_vars( $query_vars ) {
    $query_vars[] = 'starts_with';

    return $query_vars;
}
add_filter( 'query_vars', 'wpse_346729_query_vars' );

Doing this means that if the starts_with parameter is passed via the URL, it will be set on the main query, which will cause our posts_where filter to be applied to the current main query.

http://example/?post_type=staff&starts_with=A

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745180319a4615387.html

相关推荐

  • Query custom post type and custom field by URL parameters

    I have a custom post type staff. and a custom field alphabet for posts in that post type. Each staff will be assigned to

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信