Custom Query Problems
-
Hello,
I am having a problem creating a custom query on my site. Essentially I would like to achieve the same query listed as one of the examples on this page on www.ads-software.com:
https://codex.www.ads-software.com/Displaying_Posts_Using_a_Custom_Select_QueryI want to find the posts in a particular categories and then sort the results by custom field value. The website I am designing this for is https://www.theargentimes.com/agenda/. The code in question is not up there because it returns no results.
There is an Agenda (category 329) and there are 9 subcategories (987-995). On all of these category pages I want to display the entries in ascending order according to a custom field called ‘agendadate’. Here is the code i used. I am not sure what is wrong because it is exactly the same as the code in the example. The result is that no results are returned. There are definitely posts in this category with the appropriate custom key.
I use custom queries on other parts of the site but I can never get it to work with joins. Hope you can help. Thanks.
<?php // Cinema and Film module cat 987 $querystr = " SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) WHERE $wpdb->postmeta.meta_key = 'agendadate' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' AND $wpdb->post2cat.category_id IN (987) ORDER BY $wpdb->postmeta.meta_value ASC "; $pageposts = $wpdb->get_results($querystr, OBJECT); ?> <?php if ($pageposts): ?> <?php foreach ($pageposts as $post): ?> <?php setup_postdata($post); ?> <?php edit_post_link('Edit', '<small>', '</small><br />'); ?> <?php the_content(); ?> <?php endforeach; ?> <?php else : ?> <p>DOH!</p> <?php endif; ?>
- The topic ‘Custom Query Problems’ is closed to new replies.