按标题对 WordPress 帖子进行排序,忽略诸如“the”、“a”、“an”之类的文章

发布于 2024-12-22 16:31:53 字数 322 浏览 5 评论 0原文

我按标题的字母顺序对帖子进行排序,如下所示:

   <?php
      {
       $posts = get_posts($query_string . 
        '&orderby=title&order=asc&posts_per_page=-1');
      } 
      get_template_part( 'loop', 'category' );
    ?>

我想从排序中排除“the”、“a”和“an”等文章。

实现这一目标的最佳方法是什么?

谢谢!

I'm sorting my posts alphabetically by Title, like so:

   <?php
      {
       $posts = get_posts($query_string . 
        '&orderby=title&order=asc&posts_per_page=-1');
      } 
      get_template_part( 'loop', 'category' );
    ?>

I'd like to exclude articles such as "the", "a", and "an" from the sort.

What would be the best way to accomplish this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡紫姑娘! 2024-12-29 16:31:53

我不知道有什么简单的方法可以做到这一点,但你可以做到这一点,

为了实现这一点,你需要添加 自定义元字段到帖子。将其命名为 mytitle(例如)。

对于您添加的新帖子,很简单,您必须在添加帖子页面的 mytitle 自定义字段中添加修改后的标题(从标题中删除 a、an、the)。

对于旧帖子,这有点棘手,您必须编写一段 php 代码来检索帖子的标题,使用 php preg_replace 从中删除 'a'、'an'、'the' 并添加它使用类似以下内容到 WordPress 数据库的 postmeta 表:

<?php //inside loop   
$query=INSERT INTO xyz_postmeta (post_id, meta_key, meta_value) VALUES ($postid, 'mytitle' $title);
$wpdb->query('$query'); ?> 

其中 $postid 是循环内的帖子 ID,$title 是修改后的标题。

现在您已经使用自定义 mytitle 字段更新了之前的所有帖子。

现在要显示,您必须使用自定义循环(不是主题中包含的循环)。

以下是如何创建一个基本的自定义循环来显示按 mytitle 顺序排序的帖子。

$querystr = "
   SELECT wposts.*
   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
   WHERE wposts.ID = wpostmeta.post_id
   AND wpostmeta.meta_key = 'mytitle'
   AND wposts.post_type = 'post'
   AND wposts.post_status = 'publish'
   ORDER BY wpostmeta.meta_value ASC
   ";

现在您可以通过任何您想要的方式执行查询。 Wordpres 提供了多种方法来执行此操作。 这是一个链接

例如,您可以执行以下操作

$pageposts = $wpdb->get_results($querystr, OBJECT);
foreach ( $pageposts as $pagepost ) 
{
    echo $pagepost->post_title;
    //do other stuff to display content, meta etc..
}

I don't know any simple way to do that but you can do this,

For achieving this you need to add a custom meta field to the post. Name it mytitle (say).

For the new posts you add, it is simple, you have to add your modified title(removing a, an, the from the title) in the mytitle custom field in the add posts page.

For old posts it is a bit tricky, you have to write a php code to retrieve the titles of the post remove 'a','an','the' from them using php preg_replace and add it to the postmeta table of your wordpress database using something like this:

<?php //inside loop   
$query=INSERT INTO xyz_postmeta (post_id, meta_key, meta_value) VALUES ($postid, 'mytitle' $title);
$wpdb->query('$query'); ?> 

where $postid is the post id inside the loop and $title is your modified title.

Now you have updated all the previous posts with custom mytitle field.

Now to display, you have to use a custom loop (not the loop included in the theme).

Here is how you can make a basic custom loop to display posts sorted in order of mytitle.

$querystr = "
   SELECT wposts.*
   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
   WHERE wposts.ID = wpostmeta.post_id
   AND wpostmeta.meta_key = 'mytitle'
   AND wposts.post_type = 'post'
   AND wposts.post_status = 'publish'
   ORDER BY wpostmeta.meta_value ASC
   ";

Now you can execute the query by any means you want. Wordpres provides various methods to do so. Here's a link

For example you can do something like this

$pageposts = $wpdb->get_results($querystr, OBJECT);
foreach ( $pageposts as $pagepost ) 
{
    echo $pagepost->post_title;
    //do other stuff to display content, meta etc..
}
一抹淡然 2024-12-29 16:31:53

实际上,您可以通过使用 posts_orderby_request 过滤器操作 ORDERBY 子句并使用 TRIM 忽略冠词“the”、“a”和“an”来实现此目的。这是一个例子:

<?php
add_filter( 'posts_orderby_request', 'myproject_title_sort', 10, 2 );

/**
 * Ignore opening articles when sorting by Title.
 *
 * @param string   $orderby Order by parameter.
 * @param WP_Query $query   WP Query object.
 *
 * @return string
 */
function myproject_title_sort( $orderby, $query ) {
    global $wpdb;

    // Be sure to add a condition that matches your criteria (post type, archive, etc).
    // In this example, we bail early if it's an Admin query, or not a main query.
    if ( is_admin() || ! $query->is_main_query() ) {
        return $orderby;
    }

    // This is another check to see if we're on a particular post type archive.
    if ( ! $query->is_post_type_archive( 'my-post-type' ) ) {
        return $orderby;
    }

    $title_col = $wpdb->posts . '.post_title';

    // Check if we are sorting by post_title.
    // You may need to use a separate `pre_get_posts` filter to sort by "title" if not already doing so.
    if ( false === strpos( $orderby, $title_col ) ) {
        return $orderby;
    }

    $ignore  = "TRIM( LEADING 'the ' FROM LOWER( TRIM( LEADING 'a ' FROM LOWER( TRIM( LEADING 'an ' FROM LOWER( $title_col ) ) ) ) ) )";
    $orderby = str_replace( $title_col, $ignore, $orderby );

    return $orderby;
}

You can actually achieve this by manipulating the ORDERBY clause using the posts_orderby_request filter, and using TRIM to ignore articles "the", "a", and "an". Here's an example:

<?php
add_filter( 'posts_orderby_request', 'myproject_title_sort', 10, 2 );

/**
 * Ignore opening articles when sorting by Title.
 *
 * @param string   $orderby Order by parameter.
 * @param WP_Query $query   WP Query object.
 *
 * @return string
 */
function myproject_title_sort( $orderby, $query ) {
    global $wpdb;

    // Be sure to add a condition that matches your criteria (post type, archive, etc).
    // In this example, we bail early if it's an Admin query, or not a main query.
    if ( is_admin() || ! $query->is_main_query() ) {
        return $orderby;
    }

    // This is another check to see if we're on a particular post type archive.
    if ( ! $query->is_post_type_archive( 'my-post-type' ) ) {
        return $orderby;
    }

    $title_col = $wpdb->posts . '.post_title';

    // Check if we are sorting by post_title.
    // You may need to use a separate `pre_get_posts` filter to sort by "title" if not already doing so.
    if ( false === strpos( $orderby, $title_col ) ) {
        return $orderby;
    }

    $ignore  = "TRIM( LEADING 'the ' FROM LOWER( TRIM( LEADING 'a ' FROM LOWER( TRIM( LEADING 'an ' FROM LOWER( $title_col ) ) ) ) ) )";
    $orderby = str_replace( $title_col, $ignore, $orderby );

    return $orderby;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文