Posts in category (WordPress]

I wrote WordPress plugin which is sorting posts in each category via additional options (selects and input for manual 301 redirecting if is needed). The posts are displayed correctly on category lists, but after redirecting to first post in category, title sorting is not working properly.

<?php
// Dodawanie pól do panelu edycji kategorii
function custom_category_add_fields($tag) {
    // Pobieranie obecnych wartości metadanych
    $sort_by = get_term_meta($tag->term_id, 'sort_by', true);
    $sort_direction = get_term_meta($tag->term_id, 'sort_direction', true);
    $redirect_301 = get_term_meta($tag->term_id, 'redirect_301', true);
    ?>

    <tr class="form-field">
        <th scope="row" valign="top"><label for="sort_by"><?php _e('Sortuj po'); ?></label></th>
        <td>
            <select name="sort_by" id="sort_by">
                <option value="id" <?php selected($sort_by, 'id'); ?>>Id</option>
                <option value="date" <?php selected($sort_by, 'date'); ?>>Data (Domyślny)</option>
                <option value="title" <?php selected($sort_by, 'title'); ?>>Tytuł</option>
            </select>
        </td>
    </tr>

    <tr class="form-field">
        <th scope="row" valign="top"><label for="sort_direction"><?php _e('Kierunek sortowania'); ?></label></th>
        <td>
            <select name="sort_direction" id="sort_direction">
                <option value="asc" <?php selected($sort_direction, 'asc'); ?>>Rosnąco</option>
                <option value="desc" <?php selected($sort_direction, 'desc'); ?>>Malejąco (Domyślny)</option>
            </select>
        </td>
    </tr>

    <tr class="form-field">
        <th scope="row" valign="top"><label for="redirect_301"><?php _e('Przekierowanie 301'); ?></label></th>
        <td>
            <input type="text" name="redirect_301" id="redirect_301" value="<?php echo esc_attr($redirect_301); ?>" />
        </td>
    </tr>
    <?php
}
add_action('category_edit_form_fields', 'custom_category_add_fields');

// Zapis wartości metadanych
function custom_category_save_fields($term_id) {
    if (isset($_POST['sort_by'])) {
        update_term_meta($term_id, 'sort_by', sanitize_text_field($_POST['sort_by']));
    }
    if (isset($_POST['sort_direction'])) {
        update_term_meta($term_id, 'sort_direction', sanitize_text_field($_POST['sort_direction']));
    }
    if (isset($_POST['redirect_301'])) {
        update_term_meta($term_id, 'redirect_301', esc_url($_POST['redirect_301']));
    }
}
add_action('edited_category', 'custom_category_save_fields', 10, 2);

// Przekierowanie kategorii na podstawie ustawień
function custom_category_redirect() {
    if (is_category()) {
        $term = get_queried_object();
        $sort_by = get_term_meta($term->term_id, 'sort_by', true);
        $sort_direction = get_term_meta($term->term_id, 'sort_direction', true);
        $redirect_301 = get_term_meta($term->term_id, 'redirect_301', true);

        if ($redirect_301) {
            wp_redirect($redirect_301, 301);
            exit();
        }

        $args = array(
            'cat' => $term->term_id,
            'posts_per_page' => 1,
            'orderby' => $sort_by ? $sort_by : 'date',
            'order' => $sort_direction ? $sort_direction : 'desc',
        );

        $query = new WP_Query($args);
        if ($query->have_posts()) {
            $query->the_post();
            wp_redirect(get_permalink(), 301);
            exit();
        }
        wp_reset_postdata();
    }
}
add_action('template_redirect', 'custom_category_redirect');

First post in the category sorted by title is displayed OK, but others – routed by links prev/nextare not sorted, plus the problem is that the first post has links previous, next instead only prev or next (should has only ‘prev’ if the category is sorted by title DESC, and next if by title ASC – ofc next links should be routing to the sorted results) routing to the unsorted results.

Could you check my code and find the solution? Sorting by ID and Date (ASC/DESC) working ok.

New contributor

user26848284 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật