John Davidson

php - Display posts in the order of their selection

0 comments
Message:


I've offert the possibility to my client to choose one by one the related post for each post. I've set up a custom field with ACF and use this to display the choosen posts or random posts :


<?php  $terms = get_the_terms( $post->ID , 'custom-taxonomy1' );  if ( !empty( $terms ) ) :?>
<?php
$related_acf = get_field('related_group');
$rel_posts = $related_acf["related posts"];

$related_ids = array();
foreach ( $rel_posts as $rel_post) {
array_push($related_ids, $rel_post['post']);
}

global $post;
$current_post_type = get_post_type( $post );

$terms = get_the_terms( $post->ID , 'custom-taxonomy1' );
$term_ids = array_values( wp_list_pluck( $terms,'term_id' ) );
$post_count_skrn = get_theme_mod('progression_studios_media_more_like_post_count', '3');

if ( count($related_ids) > 0 ) {
$args=array(
'post_type' => array('custompost1', 'custompost2', 'post'),
'post__in' => $related_ids
);
} else {
$args=array(
'post_type' => $current_post_type,
'posts_per_page'=> $post_count_skrn,
'orderby'=>'rand', // Randomize the posts
'post__not_in' => array( $post->ID ),
'tax_query' => array(
array(
'taxonomy' => 'video-genres',
'terms' => $term_ids,
'field' => 'id',
'operator' => 'IN'
)
),
);
}

So I wonder if I can add something like :


if ( count($related_ids) > 0 ) {
$args=array(
'post_type' => array('custompost1', 'custompost2', 'post'),
'orderby'=>'XXXXX', // Ordering the posts with the order of selection
'post__in' => $related_ids
);

Best regards,
Clément



My Answer:
Here is an example code snippet that you can use to display posts in the order of their selection using Advanced Custom Fields (ACF):

php
// Get the selected posts from the custom field
$selected_posts = get_field('selected_posts');

// Check if selected posts exist
if($selected_posts) {
// Loop through each selected post
foreach($selected_posts as $post_id) {
// Get the post object
$post = get_post($post_id);

// Display the post title
echo '

' . $post->post_title . '

';
// Display the post content
echo '

' . $post->post_content . '

';
}
} else {
// If no selected posts, display random posts
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => 3 // Change the number of posts to display
);

$random_posts = new WP_Query($args);

if($random_posts->have_posts()) {
while($random_posts->have_posts()) {
$random_posts->the_post();
// Display the post title
echo '

' . get_the_title() . '

';
// Display the post content
echo '

' . get_the_content() . '

';
}
}

wp_reset_postdata();
}


In this code snippet, we first get the selected posts from the custom field 'selected_posts'. If there are selected posts, we loop through each post and display its title and content. If there are no selected posts, we display random posts using a WP_Query with 'orderby' set to 'rand'.

You can customize this code further based on your requirements and styling preferences.

Rate this post

5 of 5 based on 9465 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us