• Resolved dorich

    (@dorich)


    I’m attempting to build a glossary by using a custom post type for each glossary entry and a custom field to define what letter it should be grouped under.

    I have a template that will list all the posts in the glossary so now I need to create a compound statement to display only custom posts with a selected letter – the resulting display being, for example, all the entries under A.

    I’m new to WP and have almost no experience with php so as I work through the codex trying to find the appropriate way to create the compound query/filter, I seem to be making little progress at finding what I want.

    My question is a little vague, but what I want are perhaps some pointers as to how to tackle this requirement so that I can focus on the appropriate parts of the codex and learn what I need.

    The template for listing all the glossary entries is as follows:

    <?php
    /*
    Template Name: Glossary List
    */
    ?>
    <?php get_header(); ?>
     <div class="glossarybody">
     <h2>Glossary</h2>
     <p class="glossarybodytext">This is the glossary section of the library. Glossary Terms are listed below. Click on the word or phrase of interest to see more information.
    </p>
     </div>
                        <?php $loop = new WP_Query( array( 'post_type' => 'glossary', 'posts_per_page' => 10 ) ); ?>
    
    					<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
                                    <div> 
    
    										<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    
                                    </div>
    
                        <?php endwhile; ?>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    is this going to be a case of simply altering this piece of code or is this a more complex requirement?

    I realize there are some plugins that create glossaries but none of them appear to give the results that I want.

    Thanks.

Viewing 12 replies - 1 through 12 (of 12 total)
  • You are right! The question is a little vague! ??

    Maybe what you need is explained in the Codex under query_posts, section Custom Field Parameters.

    Thread Starter dorich

    (@dorich)

    vtxyzzy – Thanks for your response.

    The following might(?) help:

    Looking at the code I’m using that produces the list of all the posts in a particular custom post type I wonder if there is a simple way of adding the custom field so that the query became something like
    <?php $loop = new WP_Query( array( 'post_type' => 'glossary', 'meta_key'='A', 'posts_per_page' => 10 ) ); ?>

    OR should it be something like
    <?php $loop = new WP_Query( array( 'post_type' => 'glossary'&'meta_key'='A', 'posts_per_page' => 10 ) ); ?>

    Or is the whole thing going to be more complicated because I have a different post type?

    thanks for the referral to the page on queries, that’s helpful although it doesn’t show any examples of combining the custom post with a custom field to form a query. However, I’ll use it as a starting place to do some more experiments.
    Thanks

    I am guessing that your meta_key is not ‘A’, but something like ‘first-letter’. The meta_value is probably ‘A’ or ‘B’ or … .

    If that is the case, the query can be written like this:

    <?php $loop = new WP_Query( array( 'post_type' => 'glossary', 'meta_key'=> 'first-letter', 'meta_value' => 'A', 'posts_per_page' => 10 ) ); ?>

    Thread Starter dorich

    (@dorich)

    vtxyzzy

    Thanks, that works – your help is very much appreciated.

    One more question if I could:

    Is there a way to pass a parameter into the query? The use for this would be avoid having to create 26 pages, one for each letter. The goal would be to have one page that is used for whatever letter was chosen.

    Stealing from a technique used in Expression Engine I was wondering if its possible to put the letter in the link url so that the page displaying any letter would be created from the URL.
    To illustrate the question:
    Assume that I have a page that lists all the letters of the alphabet.
    If I click on A the anchor tag associated with that letter is written in the code as
    https://www.example.com/glossary/A
    When you click on ‘A” then ‘A’ is passed to the template via the url and the list of entries for the letter A is created.

    Is this possible? If so where would I look to understand how to accomplish?

    Thanks

    You can pass a query argument, but not quite in the way you showed. It should be added like this:

    https://www.example.com/glossary/?letter=A

    Then, in the code for the page, get the letter like this:

    <?php $letter = $_GET['letter'];
    $loop = new WP_Query( array( 'post_type' => 'glossary', 'meta_key'=> 'first-letter', 'meta_value' => $letter, 'posts_per_page' => 10 ) ); ?>

    Of course, you should check to make sure that a letter was passed and do something as a default if not. Otherwise, your query may behave badly. ??

    Thread Starter dorich

    (@dorich)

    vtxyzzy

    Works perfectly – Thank You!

    Regarding default behavior, is that handled by putting conditionals around the code you gave me, something like:

    <?php $letter = $_GET['letter'];
    if ($letter=="") echo "No Entries Found!";
    else
    $loop = new WP_Query( array( 'post_type' => 'glossary', 'meta_key'=> 'first-letter', 'meta_value' => $letter, 'posts_per_page' => 10 ) ); ?>

    You got it!

    You could also just default to the letter ‘A’ if you wanted to.

    Thread Starter dorich

    (@dorich)

    vtxyzzy

    Thanks for your help.

    Thread Starter dorich

    (@dorich)

    vtxyzzy

    I thought your solution was better

    You could also just default to the letter ‘A’ if you wanted to.

    But I don’t know how to test it.

    First the code I used to direct to the A page if there is nothing passed in the URL was
    if ($letter=="") $letter==A;

    Then with regard to testing I clicked on a link to produce a page that lists all posts whose title starts with C.
    Next I edited to URL to remove only the letter, so that the end of the URL is:
    glossary-list-by-first-letter?letter=

    However, that produces the following error message

    Fatal error: Call to a member function have_posts() on a non-object in /home/XXXXXXXXX…../twentyten/glossaryListLetter2v0.php on line 14

    Not sure if my code is wrong or/and I need a better test method.

    Any suggestions would be much appreciated.

    Thanks.

    I have tried what was written in this thread and it works to a degree.

    However when I use:

    <?php $letter = $_GET['letter'];
    if ($letter=="") echo "No Entries Found!";
    else
    $loop = new WP_Query( array( 'post_type' => 'glossary', 'meta_key'=> 'first-letter', 'meta_value' => $letter, 'posts_per_page' => 10 ) ); ?>

    I cannot seem to have the “No Entries Found” message, it just does not display anything but a blank page.

    I have the following code in my template

    <?php $letter = $_GET['letter'];
    if ($letter=="") echo "No Entries Found!";
    else
    $loop = new WP_Query( array( 'post_type' => 'glossary', 'meta_key'=> 'first-letter', 'meta_value' => $letter, 'posts_per_page' => 10 ) ); ?>
    
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div id="post-<?php the_ID(); ?>" class="jglossary">
    
    <li><a class="fancybox" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    
    </div>
    
    <?php endwhile; // end of the loop. ?>

    Am I doing something wrong?

    why set the custom field when u can just use substrings?

    Thread Starter dorich

    (@dorich)

    why set the custom field when u can just use substrings?

    Would you elucidate for the sake of the neophytes.

    Thanks.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘building a query of a custom post type and custom field.’ is closed to new replies.