06. april 2024 - 18:40
Der er
3 kommentarer
Vælg post rækkefølge
Jeg har en blog side hvor jeg gerne vil kontrollere hvor posten skal vises på siden
Jeg har ex.
Placering 1
Placering 2
Placering 3
Placering 4
Når jeg skriver en ny post, vil jeg gerne have lov til at vælger hvilken placering den skal have, idet jeg ex. vælger placering 2, skal posten som var på placering 2 rykke en tak ned til Placering 3 og placering 3 til 4
Det samme hvis jeg vælger at en ny post skal være på placering 1, så skal placeringer rykke
06. april 2024 - 20:22
#2
Du har en tabel med posts. Den tabel har et felt placering. Når du opretter en ny post med en placering, så indsætter du den i tabellen *og* opdaterer alle post med en højere værdi for placering.
Skal det være mere konkret må du vise noget kode.
07. april 2024 - 12:20
#3
Løste det med denne kode
<?php
function update_location_meta_on_save($post_id) {
// Check if it's an autosave or revision, then just return
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision($post_id)) {
return;
}
// Get the new location of the post
$new_location = get_post_meta($post_id, 'news_where_shown', true); // Assuming 'location' is the meta key
// Depending on the new location, update the locations of other posts
switch ($new_location) {
case 'n_one':
update_previous_posts_location('n_five', 'n_eight');
update_previous_posts_location('n_eight', 'n_oldd');
break;
case 'n_five':
update_previous_posts_location('n_five', 'n_eight', $post_id);
break;
case 'n_eight':
update_previous_posts_location('n_oldd', 'n_eight', $post_id, 'ASC'); // oldest n_oldd to n_eight
break;
}
}
function update_previous_posts_location($current_location, $new_location, $exclude_post_id = 0, $order = 'DESC') {
$args = [
'posts_per_page' => 1,
'meta_key' => 'news_where_shown',
'meta_value' => $current_location,
'orderby' => 'date',
'order' => $order,
'post__not_in' => [$exclude_post_id] // Exclude the current post
];
$posts = get_posts($args);
if (!empty($posts)) {
// Change the location of the specific post
update_post_meta($posts[0]->ID, 'news_where_shown', $new_location);
}
}
// Hook the function to post save
add_action('save_post', 'update_location_meta_on_save');