• Hi. I’m using Pods to create custom taxonomies. I have some listing pages, one for hotels, other for restaurants… and I want to order hotels listing page by hotel type (5 stars, 4 stars, 3 stars…). hotel is one taxonomy and hotel_type is another taxonomy, but related with “hotel” taxonomy. How can I do this using wp_query()?

    Thanks

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    I take it the actual property you want to list is a post of some type, to which various taxonomy terms are assigned. Unless these properties are actually “post” post type, you’ll need to specify that in your query. That and getting only posts with a particular taxonomy term is possible with WP_Query. Where you run into trouble is ordering the found posts by taxonomy term name. There is no such orderby argument accepted by WP_Query. You essentially need to write and use your own SQL query.

    You would then use $wpdb methods to execute your query. You can then loop through the results and generate output much like the usual WP loop. But there is one big caveat. The standard WP loop relies on WP_Query methods like have_posts() and the_post(). These are not available when you use $wpdb methods. Furthermore, template tags like the_permalink(), the_title(), etc. will not work in your loop unless you specifically prepare the current post object with setup_postdata().

    Thread Starter manuelgonzalez

    (@manuelgonzalez)

    Hi,

    for a better understanding, I attach a few screenshots. Thanks.

    https://ibb.co/coRbFx
    https://ibb.co/iLP7Nc
    https://ibb.co/fApwFx
    https://ibb.co/emidTH

    Moderator bcworkz

    (@bcworkz)

    OK, it looks like you want to query for the “tipo_hotel” post type. That’s fine. What I said previously still holds true, you cannot order tipo_hotel posts by taxonomy term names with WP_Query. You would need to compose a SQL query to get posts ordered by term name.

    The other thing you could do is use WP_Query to get the posts, then use PHP’s usort() to arrange the returned posts in desired order. Depending on how this is done, it can be anywhere from inefficient to extremely inefficient. Using SQL would be much better.

    Thread Starter manuelgonzalez

    (@manuelgonzalez)

    Thanks bcworks. Maybe I need some help with this query in wordpress (I usually do SQL querys outside WordPress, but I’m not very familiar with querys with taxonomies and pods).

    Moderator bcworkz

    (@bcworkz)

    Understandable. The taxonomy/term/object relationship is a bit convoluted. I’ve struggled with this as well. I can offer what I ended up with. You will need to adapt it to your specific situation. At least it gives you a good head start.

    //list all posts & portfolios w/ categories ordered by category name
    global $wpdb;
    $query = $wpdb->get_results( "SELECT * FROM $wpdb->posts AS p
    	LEFT JOIN $wpdb->term_relationships AS r ON (p.ID = r.object_id)
    	INNER JOIN $wpdb->term_taxonomy AS x ON (r.term_taxonomy_id = x.term_taxonomy_id)
    	INNER JOIN $wpdb->terms AS t ON (r.term_taxonomy_id = t.term_id)
    	WHERE p.post_type IN ('post', 'portfolio') 
    	AND p.post_status = 'publish'
    	AND x.taxonomy = 'category'
    	ORDER BY t.name ASC, p.post_date DESC;"
    );
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Order by a custom taxonomy (Pods) with wp_query()’ is closed to new replies.