2

I'm working in Wordpress with a custom post type of ''publications." Originally, each "publication" was assigned to a single "category," and getting them to display by category was simple...

$args = array( 'post_type' => 'publications', 'meta_key' => 'publications_category', 'orderby' => 'meta_value', 'order' => 'ASC');
$loop = new WP_Query( $args );

Then the client decided they wanted to assign the same "publication" to multiple "categories," and my meta_key became an array. And now I can't get the "publications" to sort alphabetically by "category."

Because my meta_key is now an array, I removed the 'meta_key', 'order_by' and 'order' from the query. Here's what I have been testing...

<?php
$args = array( 'post_type' => 'publications');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$cp_category = get_post_meta($post->ID, 'publications_categories', true); // assign a var to the array
foreach ($cp_category as $category) { // iterate through the array
the_title();  // print the title
echo '<br><br>' . $category . '<br><br>'; //  print the category
}
endwhile;
?>

I have tried various sort() functions on the array with not luck.

There are six categories. The values are strings: 'cabg_vs_opcab', 'high_risk_patients', 'mortality_and_morbidity', 'stroke', 'clampless_beating_heart', and 'economics.'

Here is what the output looks like. I added a print_r() to diplay the array above each entry:

http://offpump.com/clinical-publications/

All I want to do is order the entries by their category so I'm hoping there is a way to do that.

Thanks for any help or suggestions!

2 Answers 2

2

You should not sort them alphabetically after retrieving them from the database.

To avoid any duplicate processing you should fetch them pre-sorted.

Quick search led me to the syntax for wordpress queries:

$args = array(
        'post_type' => 'post',
        'meta_key' => 'pb_issue_featured',
        'orderby'   => 'meta_value',
        'order' => 'DESC',
        'post_status' => 'publish',
        'posts_per_page' => $posts,
        'paged' => $paged,
        'meta_query' => array(
            array(
                'key' => 'headline',
                'value' => 1,
                'compare' => '!=' 
                )
            )
        );
$q = new WP_Query($args);

https://wordpress.stackexchange.com/questions/109849/order-by-desc-asc-in-custom-wp-query

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for helping. Just to be clear, my meta_key (publications_categories) is an array (from a checkbox group) of one or more categories (cabg_vs_opcab, high_risk_patients, etc.). I added a meta_query as you suggested, but it only worked if I used the 'LIKE' comparison. Comparing with "=" or "IN" did not work. That has me stumped b/c I need to get ONLY the one category, and "LIKE" also fetches any other category in the array with the one I want. And finally, is there a way to get all the categories in one query? Not just one at a time?
That is the goal isn't it :) I'm not as familiar with wordpress so my suggestion is to explore a direct sql solution, use it or after finding the necessary syntax convert it into wp_query format.
0

So since the only 'compare' parameter that worked for me was "LIKE" I ended up using it. The page/template has a loop for each "LIKE" query -- six in all -- and it works fine b/c there are not that many posts.

I'm determined to find a better solution, however, and will come back and post it when I get there.

Thanks to all who helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.