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.
user26848284 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.