WordPress.org

Ready to get started?Download WordPress

Forums

Would appreciate help with sorting a custom field on tag and category pages (1 post)

  1. isbrowny
    Member
    Posted 1 year ago #

    Hello! I'm not too experienced with coding, but I've been learning a lot. I've been trying to get this to work for several weeks now, so I thought I'd ask here. Thus far, this community has been an amazingly huge help.

    Problem: I'm working on an events website and I can't sort the events on each page (for both the tags and category pages) by the custom field "event _date". I've had success using the code below on the home page of the site, but it does not work on any other page.

    Problem Example: Right now, if I'm on a page for a tag named "Rock" or "Dance" or "Punk" it currently displays all the posts on the site (even those which don't contain the tag for that specific tag page).

    Goal: I would like for the posts in all the tag and category pages to be sorted by the custom field "event_date", with pagination. So if I'm on a tag page called "Pop" all the posts tagged with "Pop" would be organized by "event_date."

    Current code: This is the exact code I have in my "archive.php" page:

    <?php
    if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
    				}
    elseif ( get_query_var('page') ) {
        $paged = get_query_var('page');
    
    } else {
        $paged = 1;
    }
    query_posts( array(
            'posts_per_page' => 5,
    	'meta_key' => 'event_date',
    	'meta_value' => date('Y-m-d'),
    	'meta_compare' => '>',
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
     'paged' => $paged)); ?>
    
      <div class="section entry" id="entry<?php the_ID(); ?>">
        <?php if(have_posts()) : ?>
        <?php while(have_posts()) : the_post(); ?>
        <div <?php post_class(); ?>>
          <div class="post">

    Thank you for your attention!

    [ Please do not bump, that's not permitted here. ]

Topic Closed

This topic has been closed to new replies.

About this Topic