mercredi 15 avril 2020

Wordpress: sort custom post type posts

So I have a custom post type called members. Here members are added via advanced custom fields.

The members I display in a template page.

now I want them sorted under ABC. somewhat like this: example for the users "In the picture, I made only the first few letters visible for privacy reasons."

and I want it so if there is no user with a specific letter it won't show the letter above. like if there were no users with the letter B it would just go from A to C

I'm not really good with PHP but I made like a small beginning to an if statement:

<?php
$current_character = '';
while (loop) {
    $first_character = code to get first character out of name;
    if ($current_character != $first_character) {
        echo $first_character;

        $current_character = $first_character;
    }
    echo $name;
}
?>

I know this is 100% not gonna work yet. but it might be useful for u guys to help me :D

The code I have for displaying my members on the template page is:

$member_type = (isset($member_type) ? $member_type : 'seb');
$post_type = $member_type == 'seb' ? array('companies', 'members') : 'companies';
// prepare arguments
$args = array(
    'post_type'                 => 'members',
    'orderby'                   => 'metavalue',
    'meta_key'                  => 'contactperson',
    'order'                     => 'ASC',
    'post_status'               => 'publish',
);

// create a query based on arguments
$query = new WP_Query($args);

while ($query->have_posts()):
    $query->the_post();

plus I have:

<section>
    <p>
        <?php echo (get_field('title') ? get_field('title') . ' ' : '') ?>
        <?php the_field('contactperson') ?>
    </p>
</section>

the users are sorted and displayed by contactperson

Aucun commentaire:

Enregistrer un commentaire