最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

code - Reorder a page of posts of a certain taxonomyvalue by a custom field

programmeradmin2浏览0评论

I want to re-order a page of posts of a certain taxonomy/value by an added Advanced Custom Fields PRO field, instead of the default (ordered by post-date).

We have a WP site with a custom taxonomy, added like this in a taxonomies.php file:

$args = array(
    'hierarchical'      => false,
    'labels'            => $labels,
    'public'            => true,
    'show_admin_column' => true,
    'rewrite'           => array( 'slug' => __( 'article-type', 'sage' ) ),
);
register_taxonomy( 'article_type', array( 'post' ), $args );

The article types are set by tags in wp-admin/edit-tags.php?taxonomy=article_type e.g.

Name: Webinars, Slug: webinar
Name: Case Studies, Slug: case-studies
Name: Opinion, Slug: opinion

So we can then show a list of posts with a certain taxonomy/value like this:

/
/
/

And this works fine, but we need to show the webinars content ordered by a custom field, 'webinar_date', instead of when it was posted.

Webinar date is added in Advanced Custom Fields PRO like this:

Order: 1
Label: Webinar date
Name: webinar_date
Type: Date Time Picker

I'm quite new to WP development, so I'm not sure exactly how to do this!

I wonder if I should add an action like

add_action('pre_get_posts','reorder_webinars');

Where that function checks if the item matches the correct taxonomy and value and then reorders the results, but I honestly don't know how to do this.

It's a Bedrock/Sage based site, if that matters.

Thanks for any help.

[edit]

So there's this code already present, I wonder if I could add to this?

add_filter( 'pre_get_posts', function () {
  global $wp_query;

  // article type
  if ( isset( $wp_query->query['article_type'] ) ) {
    set_query_var( 'posts_per_page', 12 );
  }
} );

So in the check for article type I could check that the 'type' of 'article_type' is 'webinar', then alter the $wp_query? How can I check this value? And how to alter the order at this point?

I want to re-order a page of posts of a certain taxonomy/value by an added Advanced Custom Fields PRO field, instead of the default (ordered by post-date).

We have a WP site with a custom taxonomy, added like this in a taxonomies.php file:

$args = array(
    'hierarchical'      => false,
    'labels'            => $labels,
    'public'            => true,
    'show_admin_column' => true,
    'rewrite'           => array( 'slug' => __( 'article-type', 'sage' ) ),
);
register_taxonomy( 'article_type', array( 'post' ), $args );

The article types are set by tags in wp-admin/edit-tags.php?taxonomy=article_type e.g.

Name: Webinars, Slug: webinar
Name: Case Studies, Slug: case-studies
Name: Opinion, Slug: opinion

So we can then show a list of posts with a certain taxonomy/value like this:

https://mywebsite.co.uk/article-type/webinar/
https://mywebsite.co.uk/article-type/case-studies/
https://mywebsite.co.uk/article-type/opinion/

And this works fine, but we need to show the webinars content ordered by a custom field, 'webinar_date', instead of when it was posted.

Webinar date is added in Advanced Custom Fields PRO like this:

Order: 1
Label: Webinar date
Name: webinar_date
Type: Date Time Picker

I'm quite new to WP development, so I'm not sure exactly how to do this!

I wonder if I should add an action like

add_action('pre_get_posts','reorder_webinars');

Where that function checks if the item matches the correct taxonomy and value and then reorders the results, but I honestly don't know how to do this.

It's a Bedrock/Sage based site, if that matters.

Thanks for any help.

[edit]

So there's this code already present, I wonder if I could add to this?

add_filter( 'pre_get_posts', function () {
  global $wp_query;

  // article type
  if ( isset( $wp_query->query['article_type'] ) ) {
    set_query_var( 'posts_per_page', 12 );
  }
} );

So in the check for article type I could check that the 'type' of 'article_type' is 'webinar', then alter the $wp_query? How can I check this value? And how to alter the order at this point?

Share Improve this question edited Sep 16, 2021 at 10:00 Ralpharama asked Sep 16, 2021 at 8:36 RalpharamaRalpharama 1035 bronze badges 4
  • you can do this by modifying the main query with the pre_get_posts filter and the parameters listed in the official docs, your suspicion is correct – Tom J Nowell Commented Sep 16, 2021 at 8:55
  • Thanks @tom-j-nowell any pointers on how exactly? I'm afraid I've read the docs and I'm not exactly sure how to check the right values and then actually do the reordering :( as I say I'm really quite new to this and so on a steep learning curve! Any tips gratefully received. – Ralpharama Commented Sep 16, 2021 at 10:02
  • check orderby in the WP_Query docs, You can also add to that filter or add a second – Tom J Nowell Commented Sep 16, 2021 at 10:20
  • Thanks Tom, I will give it a look. – Ralpharama Commented Sep 16, 2021 at 10:49
Add a comment  | 

1 Answer 1

Reset to default 0

Well, I did it like this. Maybe it will help someone.

add_filter( 'pre_get_posts', function () {
  global $wp_query;

  // article type - change posts per page. If webinars then change order to webinar_date
  if ( isset( $wp_query->query['article_type'] ) ) {
    set_query_var( 'posts_per_page', 12 );
    if ( $wp_query->is_tax('article_type', 'webinar') ) {
      $wp_query->set( 'meta_key', 'webinar_date' );
      $wp_query->set( 'orderby', 'meta_value' );
      $wp_query->set( 'order', 'DESC' );
    }
  }
} );
发布评论

评论列表(0)

  1. 暂无评论