Support » Fixing WordPress » Sort by custom fields: orderby is only showing the latest page instead of all

  • Resolved apax27

    (@apax27)


    First off, I am a WordPress newbie… a designer first, developer second- the side of my brain that makes things “pretty” overpowers the side that understands how programming and code works!!!

    I have been searching these boards for hours and playing with my template’s code and I just can’t seem to get this right…

    I am designing a website that is basically a business directory for a local area. I have set up custom fields for each business’ page:
    Business Category, Subcategory, Address, Phone, website, etc.

    Then on the main page I want to have all of the categories sorted in alphabetical order, followed by subcategories, followed by the businesses underneath, like this:

    AUTOMOTIVE
    Auto Accessories
    Auto Barn
    Auto Zone
    Auto Parts
    Jack’s Auto Parts Inc
    New Cars
    Johnson Chevrolet
    Smith’s Ford
    Used Cars
    Tim’s Used Cars Inc

    ELECTRONICS
    Audio
    Tom’s Stereo Equipment
    Computers
    Computer World
    Telephones
    Mobile Phones Inc.

    Here’s what I came up with after searching and testing:

    <?php while (have_posts()) : the_post(); ?>
    		<?php query_posts($query_string . '&meta_key=business_category&orderby=meta_value'); ?>
    		<h4><?php echo get_post_meta($post->ID, "business_category", true) ?></h4>
    		<div class="list">
    		<?php query_posts($query_string . '&meta_key=sub-categories&orderby=meta_value'); ?>
    		<h6><?php echo get_post_meta($post->ID, "sub-categories", true) ?></h6>
    			<div class="list-img">
    				<p id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="" title="Permanent Link to <?php the_title(); ?>"><?php the_content(); ?></a></p>
    			</div>
    			<div class="list-title">
    			<p id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></p>
    			<p><?php echo get_post_meta($post->ID, "business_address", true) ?><br />
    			<?php echo get_post_meta($post->ID, "business_town", true) ?></p>
    			</div>
    		</div>
    		<?php endwhile;
    wp_reset_query();
    // Restore global post data
    ?>

    But it only shows 1 category and only the most recent business listing.

    What am I missing?

    Thanks in advance!!!!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter apax27

    (@apax27)

    oh forgot to mention, each listing will be a link to that page, and I want the categories to auto-generate as more categories are added, if that makes any sense.

    (If my client thinks of a new category, I want that category to automatically show up on this main page when it is added via custom fields)

    In the first place, you have two calls to query_posts() within the Loop (while (have_posts()) … ). Each call to query_posts() starts a new Loop, overwriting the current one, so this approach will not work.

    You will need a custom query to select the post data, the business category and the sub category in a single query and then have one Loop to process the results.

    This is probably too complex to address in this forum. If you wish, contact me here and I will try to help.

    Thread Starter apax27

    (@apax27)

    Thank you so much for your help 🙂 After playing around with it, I sort of got it to function the way I’d like… Here is my code

    [CSS moderated as per the Forum Rules. Please just post a link to your site.]

    It’s listing the categories in alphabetical order, but this is the structure (sample category names are in bold)

    Automotive
    Auto Parts Inc – excerpt from page

    Automotive
    Auto Zone – excerpt from page

    Clothing and Accessories
    Beth’s Boutique – excerpt from page

    Clothing and Accessories
    Dresses for Less – excerpt from page

    The structure I am trying to achieve is

    Automotive
    Auto Parts Inc – excerpt from page
    Auto Zone – excerpt from page

    Clothing and Accessories
    Beth’s Boutique – excerpt from page
    Dresses for Less – excerpt from page

    See the code in this pastebin.

    Thread Starter apax27

    (@apax27)

    Thank you…

    I can’t seem to get that code to work. Is inside the loop or does it replace the loop?

    I am actually not doing subcategories anymore because it was getting too complicated. Is there any way I can modify the code I already have- it is laid out just the way I want it, the only problem was the way it was structured.

    Sorry for being such a newb… this is like trying to learn another language! Thanks for your patience!!

    Everyone is a newb at the start.

    I don’t believe that the code you have can easily be modified to work. You need a custom query to get the sort order correct.

    Then, in order to get the grouping to work, you have to put in tests to see if the post currently being processed belongs to the current group and start a new group if not.

    The code I posted replaces the loop. It would replace all of the code you showed above.

    Are you going to add the subcategories back in or not?

    Thread Starter apax27

    (@apax27)

    It’s actually going to be laid out as

    Town-> Category -> Business

    But I had a different template and page for each town… each template starts with:

    <?php query_posts('post_type=page&meta_key=town&meta_value=Anytown'); ?>

    But maybe the way you did it we could replace category with town, and then subcategory with category… that may simplify things a bit. This is actually for a client so once I figure out how to do it, I have to teach the client how, so the less pages we have the better.

    It should certainly work to use Town instead of Business Category, and Business Category instead of Subcategory.

    Just change the meta_key values that are in the pastebin code.

    Also, I just noticed that you are using Pages instead of Posts. You need to change the post_type in the pastebin code as well.

    Thread Starter apax27

    (@apax27)

    ahh! It worked! Silly me- I should have figured that one out!

    thank you thank you!!!

    I just need to style my results- but I’m sure I can figure that one out.

    You are awesome for taking the time to help. Much appreciated 🙂

    Thread Starter apax27

    (@apax27)

    This is to help others looking to do the same thing
    The following code will separate your custom fields into categories and subcategories….

    I searched day and night for this solution so maybe this code can help someone else out. Thanks again!

    [Code moderated as per the Forum Rules. Please use the pastebin]

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Sort by custom fields: orderby is only showing the latest page instead of all’ is closed to new replies.