• I hope this question makes sense.

    I am designing a page for a second hand bookshop, It will be linked to in a weekly email newsletter containing that weeks books added to the store, so subscribers can keep updated on rare books that are available.

    At the moment this is sent out as a pdf organised in broad categories. eg history, archaeology, antiquarian etc. With about 50-70 books on it.

    The website I am building use a custom taxonomy to allow browsing of book categories.

    For my weekly updates page I really need to have all books listed on one page with a heading for each category.

    So using;

    $args = array(
    	'date_query' => array(
    		array(
    			'column' => 'post_date_gmt',
    			'after' => 'Monday 1 week ago',
    		),
    	),
    	'posts_per_page' => -1,
    	'post_type' => 'product',
    );

    writing a loop that gets all books added after last monday if fine. But after reading https://wordpress.stackexchange.com/a/14309/53985 it seems that taxonomies are not a good way of listing posts like this. It seems I can do it but will have to;

    1. Get the top level taxonomies.
    2. for each taxonomy find all books added ‘after’ => ‘Monday 1 week ago’
    3. If there are any new books in that taxonomy publish the title and the list of books.
    4. repeat for all taxonomies.

    I am not sure exactly how to do this, and I am even less sure it is a good idea. Seeing as the list will go out to subscribers, it will probably be the most visited page on the site and peak the usage of the website as a whole each day the email is sent out (as everyone checks for new arrivals on the same day) and using complicated multiple loops may slow the page down???

    So my question. Is this a good way of doing it? Will it be ok as there will only be ~50 posts to go through each time? or should I be looking at some other method?

    Maybe I should add a custom meta field “Topic” to each book listing that is the same as each top level taxonomy and use this field to sort my book list? Would this be an improvement? Would that make it faster / easier? If I do this, and order the books by my Topic field. Would it be possible echo out the topic each time a new category starts as a title for each set of books, or would I have to echo the topic in each post, so people can see what category it belongs to?

    Thank you for taking the time to read this. I know it is a rather complicated question, and I really appreciate any advice anyone can give me.

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

    (@bcworkz)

    If you are concerned about the site bogging down due to many requests for the same page, you should consider using one of the caching plugins available. Note that WP does do some caching on its own, so things may not be as bad as they seem. I don’t actually know if the WP cache will deal with this specific issue or not.

    Even with good caching, it’s always good practice to minimize the amount of queries required for any page. Doing multiple queries can be quite easy to code, but as you suspect, it is highly inefficient. The solution is to order the results in such a way that the loop can detect a change in taxonomy or meta term and output the intermediate markup for the subsequent group before continuing the loop. One query should then suffice for all topics.

    The main problem with using a topic taxonomy is WP_Query does not have an easy way to order by taxonomy term. Such a query is possible by filtering the query clauses, but the resulting query string is beyond my skills to put together. It’s not all that complicated, I’m just that bad at SQL queries ??

    If the topics were managed as custom meta fields, WP_Query does have the ability to order by these terms, so there is no need for tricky filtering and special SQL query strings, the dirty work is done for us. So as mentioned, it’s only a case of the loop detecting when the meta term changes so the next group’s header can be output.

    Thread Starter jasperg

    (@jasperg)

    @ bcworkz thanks for your help. This is exactly what I suspected. I was just about to reply with my solution which dose exactly as you suggested. As a side, the reason I am reluctant use a cashing plugin for this page is that it will be a list of individual second hand books that (hopefully) be sold throughout the day/week so need to be up to date so as not to disappoint too many people trying to buy items that are already sold. I am wandering if I could cash the page still but add un-cashed meta data in the footer that lists sold items, so I could then use javascript to mark them as sold? but I have not looked into how to cash all but the footer. Is this posable/desirable?

    After settling on the option you describe (using custom meta field to list in order and adding it as a title)

    I used WP_Query to order by a field wpcf-topic

    Then declared variable $last_topic before the loop with nothing in it and then in the loop at the start $topic=(get_post_meta( get_the_ID(), 'wpcf-topic', true )); and then

    if ($last_topic !== $topic){ echo "<h1>"  .  $topic   .  "</h1>";
    }

    then at the just before the end of the loop

    $last_topic=$topic;

    So now <h1>Topic</h1> is not shown till a new topic comes up
    If any one reading this want the full code that I used, I posted a more detailed reply to myself on a more more specific question I asked on of how to do this on

    https://wordpress.stackexchange.com/questions/151987/create-a-list-of-posts-with-topic-headdings

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Create a list of posts with topic heddings’ is closed to new replies.