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

categories - Get_term_meta() does not work with pre_get_posts()

programmeradmin0浏览0评论

I am trying to get custom term meta value for a certain category using get_term_meta() function. This value is stored in the wp_termmeta table. Then, based on that value, I want to modify the main query for a certain category archive page (sort the posts by date ascending). However, the pre_get_posts() hook that I am using does not allow me to do so. I tried all the ways that i am aware of, however, it is still not working

Here is my code:

function my_new_category_order( $query ) {
    //get the category id
    $cat_id = get_query_var('cat');
    //get the custom meta value
    $post_order = get_term_meta($cat_id, 'post-order', true);
    //The value is shown two times. First, it is false, then it is "oldest"
    var_dump($post_order); 

    //Cannot go inside the if statement because $post_order is false!
    if ( $post_order && $query->is_category($cat_id) && $query->is_main_query() ) {
        $query->set( 'order', 'ASC' );
        //$post_order is false here
        var_dump($post_order);
        //$cat_id is empty string
        var_dump($cat_id);
    }


}
add_action( 'pre_get_posts', 'my_new_category_order' );

As a result, I am not able to go inside the if statement and reorder the posts.. The rest of the code is working fine but the term meta value remains undefined. What am I doing wrong?

I am trying to get custom term meta value for a certain category using get_term_meta() function. This value is stored in the wp_termmeta table. Then, based on that value, I want to modify the main query for a certain category archive page (sort the posts by date ascending). However, the pre_get_posts() hook that I am using does not allow me to do so. I tried all the ways that i am aware of, however, it is still not working

Here is my code:

function my_new_category_order( $query ) {
    //get the category id
    $cat_id = get_query_var('cat');
    //get the custom meta value
    $post_order = get_term_meta($cat_id, 'post-order', true);
    //The value is shown two times. First, it is false, then it is "oldest"
    var_dump($post_order); 

    //Cannot go inside the if statement because $post_order is false!
    if ( $post_order && $query->is_category($cat_id) && $query->is_main_query() ) {
        $query->set( 'order', 'ASC' );
        //$post_order is false here
        var_dump($post_order);
        //$cat_id is empty string
        var_dump($cat_id);
    }


}
add_action( 'pre_get_posts', 'my_new_category_order' );

As a result, I am not able to go inside the if statement and reorder the posts.. The rest of the code is working fine but the term meta value remains undefined. What am I doing wrong?

Share Improve this question edited Sep 30, 2019 at 6:39 Badan asked Sep 29, 2019 at 18:24 BadanBadan 2251 silver badge7 bronze badges 9
  • As you can see, $query is undefined and returns null when accessed. 'edit_category' means a category has been edited, a save operation. You are not querying anything. Altering sort order happens in 'pre_get_posts'. I don't understand what you are trying to do, or may be just trying to do two separate things in one action. Maybe split the two concerns. – Knut Sparhell Commented Sep 29, 2019 at 19:38
  • I edited my question and uploaded the modified code. I am now using the pre_get_posts hook. However, it is still not working. I believe it is so, because the query runs before I am able to fetch data from wp_termmeta table.. how can i make it work? Please check my modified question. – Badan Commented Sep 29, 2019 at 22:13
  • I don't believe $post_order can change within the scope of a function without being set. Dump $cat_id and is_category and is_main_query too. Also set he query order without a condition to test it. – Knut Sparhell Commented Sep 29, 2019 at 22:32
  • Should not be a problem to get meta, any time, anywhere. 'post-order' is correct key, not 'post_order'? - have to ask... – Knut Sparhell Commented Sep 29, 2019 at 23:00
  • Yes, the key is post-order. Might be due to the fact that pre_get_posts gets the data before the actual query? – Badan Commented Sep 29, 2019 at 23:06
 |  Show 4 more comments

1 Answer 1

Reset to default 0

I finally, finally figured it out! I was not able to get the category id in the first place. Strangely, get_query_var('cat') is not working. Here is the way that worked for me, in case someone else also struggles with this:

function my_new_category_order( $query ) {
    $category = get_queried_object();
    $cat_id = $category->term_id;
    $post_order = get_term_meta($cat_id, 'post-order', true);
    //Now everything works!
    if ( $post_order =='oldest' && $query->is_category($cat_id) && $query->is_main_query() ) {
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'my_new_category_order' );
发布评论

评论列表(0)

  1. 暂无评论