2
votes

This is my first post on stack overflow!

I am a WordPress beginner and have almost completed developing my first theme that involves using basic property listings.

I have already tried to solve this problem by checking here..

..as well as many google searches. and I cant seem to find anything that will help me.

Im having trouble displaying posts from a custom post type called 'tfg-properties' in the order that i need them to appear.

Custom Taxonomy - 'featured-properties' - functions.php

register_taxonomy('featured-properties', 'tfg-properties', array(
        "hierarchical" => true,
        "label" => "Featured Properties",
        'update_count_callback' => '_update_post_term_count',
        'query_var' => true,
        'rewrite' => array( 
            'slug' => 'featured-property',
            'with_front' => false ),
        'public' => true,
        'show_ui' => true,
        'show_tagcloud' => true,
        '_builtin' => false,
        'show_in_nav_menus' => false)
    );

I have created a custom taxonomy for this post type called 'featured-properties' what I want to do is have the posts from taxonomy - 'featured-properties' display before the other posts.

I am able to achieve this using categories but since I am also incorporating a blog this will not work..

Here is how I am querying my posts..

Query posts by category id - page.php

// Get all posts from featured properties
    $catId = get_cat_ID('Featured Property');

    query_posts('post_type=tfg-properties&cat=' . $catId);

// begin the loop
while( have_posts() ): the_post(); ?>
    <li class="single-prop">
        ...
    </li>
<?php endwhile; ?>

// Get all posts from featured properties

    query_posts('post_type=tfg-properties&cat=-' . $catId);

// begin the loop
while( have_posts() ): the_post(); ?>
    <li class="single-prop">
        ...
    </li>
<?php endwhile; ?>

Is there any way I can sort the custom posts with custom tax to display before the other posts?

Thanks in advance!

1

1 Answers

-1
votes

You could use two loops - the first loop only displaying the taxonomy - then the second displaying all others, excluding the taxonomy.

<?php 
$catId = get_cat_ID('Featured Property');
global $post; 

rewind_posts();
$query = new WP_Query(array(
 'cat' => $catId, 
 'post_type' => 'tfg-properties',
));
while ($query->have_posts()) : $query->the_post(); 
?>

<li class="single-prop"></li>

<?php 
endwhile; 
wp_reset_postdata();
?>

<?php 
rewind_posts();
$query = new WP_Query(array(
 'cat' => '-' . $catId, 
 'post_type' => 'tfg-properties',
));
while ($query->have_posts()) : $query->the_post(); 
?>

<li class="single-prop"></li>

<?php 
endwhile; 
wp_reset_postdata();
?>