• Resolved stevechatterton

    (@stevechatterton)


    Hey, it’s me, the guy with the book site again: https://www.thelitreview.com

    We use Exec-PHP extention a lot on the site, and we have a few pages that use custom WP_Query functions, many of which have had invaluable help from people in these forums, to do things like listing best rated books in a certain genre (category):
    https://www.thelitreview.com/the-lit-reviews-top-10-list

    Here’s the latest thing we want to do and I have no idea if it’s possible => We want to be able to have alphabetized listings of all book reviews by either book title or author’s name, but the only way I can think to do it is by forcing regular expressions into the $args array in WP_Query() to sift through the titles of the posts to display the relevant lists.

    We’re already using custom fields to display ratings on landing pages & search results, so that’s out unless there’s an easy way to have multiple custom fields where only a select few display (right now we use the_meta() which I believe doesn’t accept any arguments – would we be better off exploring get_post_meta()?).

Viewing 5 replies - 16 through 20 (of 20 total)
  • Thread Starter stevechatterton

    (@stevechatterton)

    Any chance I could coax you into posting your revised code, because I seem to get an error with everything I try.

    By the way, I do really appreciate all the help. Something’s just not clicking here.

    Of course I use different values …

    <?php
    global $wpdb;
    $meta_key = 'cf1';
    $start_value = 'C';
    $end_value = 'D'; 
    
    $postids = $wpdb->get_col($wpdb->prepare("
    SELECT DISTINCT post_id
    FROM        $wpdb->postmeta
    WHERE       (meta_key = %s)
                AND (UPPER(meta_value) >= %s AND UPPER(meta_value) < %s)",$meta_key, $start_value, $end_value)); 
    
    if ($postids) {
      $args=array(
        'post__in' => $postids,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo 'List of Posts';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        endwhile;
      }
      wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?>

    Thread Starter stevechatterton

    (@stevechatterton)

    Whatever they’re paying you, it’s not enough!

    I was trying to declare it within the function (at the advice of a rather old book). But that, indeed, does work, and now I’m going to stop bothering you for a good long while. Know that I’m eternally grateful & have yourself a great weekend.

    You are welcome. Marking resolved.

    Note: all volunteers in these forums!

    Thread Starter stevechatterton

    (@stevechatterton)

    Well, if you write a book of your coolest WP tricks I’ll be first in line to buy it.

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘WP_Query with RegEx: Is it possible? Custom Fields better?’ is closed to new replies.