• Resolved headbang3r

    (@headbang3r)


    Hi, I tried a lot of different ways to order my fields but I just keep hitting the same wall : It looks like I can’t order them alphabetically ??

    Here is the website on which I’m working : https://www.juraimmobilier.ch/
    The area where I’m trying to do my sorting is on the botttom right corner, the search box.
    If you click on “TYPE D’OBJET” you can see there’s a list not sorted in any way.

    And the way I’m trying to sort this list (the CPT is a really simple one with only a title), and has a get_reverse_related to display only the types of objects related to the rents.

    <?php
    $type_ids = get_posts(array(
    	     'posts_per_page' => '-1',
    	     'fields' => 'ids',
    	     'post_type' => 'types-de-biens',
    	     'meta_key' => 'title',
    	     'orderby' => 'meta_value',
    	     'order' => 'ASC'
    	));
    	echo "<OPTION selected disabled>type d'objet</OPTION>";
    	foreach($type_ids as $id):
    		if(CFS() -> get_reverse_related($id, array('post_type' => 'objets-a-louer'))):
    			$type_id_sorted[] = $id;
    		echo "<OPTION>" . get_the_title($id);
    		endif;
             endforeach;
    ?>

    What am I doing wrong here ? I’m really runing out of ideas and can’t seem to find the correct solution.

    Help would be really welcome, thanks in advance.
    Jonathan

    https://www.ads-software.com/plugins/custom-field-suite/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Matt Gibbs

    (@mgibbs189)

    The “get_reverse_related” method returns an array of unsorted post IDs. If you need them sorted, then pass them into a WP_Query instance, via the “post__in” argument.

    $post_ids = CFS()->get_reverse_related( ... );
    $query = new WP_Query( array(
        'post_type' => 'blah',
        'post_status' => 'publish',
        'post__in' => empty( $post_ids ) ? array( 0 ) : $post_ids,
        'orderby' => 'title',
        'order' => 'ASC',
    ) );
    

    Cheers

    Thread Starter headbang3r

    (@headbang3r)

    Hi, thanks for your answer but I still can’t make it work…

    <?php
    $post_ids = CFS()->get_reverse_related($id, array('post_type' => 'objets-a-louer'));
    $query = new WP_Query( array(
        'post_type' => 'types-de-biens',
        'post_status' => 'publish',
        'post__in' => empty( $post_ids ) ? array( 0 ) : $post_ids,
        'orderby' => 'title',
        'order' => 'ASC',
    ) );
    echo "<OPTION selected disabled>type d'objet</OPTION>";
    foreach($query as $id):
    	echo "<OPTION>" . get_the_title($id);
    endforeach;
    ?>

    This is what I have now but it gives me main nav titles

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Can't sort my fields alphabetically’ is closed to new replies.