Forum Replies Created

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter bradical911

    (@bradical911)

    thank you bcworks, your advice is much appreciated – the main thing for me is being pointed in the right direction…will post back results if successful

    Thread Starter bradical911

    (@bradical911)

    Im now reading this http://codex.wordpress.org/Class_Reference/wpdb
    sounds like the right place to start

    Thread Starter bradical911

    (@bradical911)

    thanks, I’m fine with “wordpress” PHP but SQL commands not so as I’m more a designer than dev although interested to learn more, I found some code I’ve started to customise but I’m not really sure what I’m doing

    The aim is to order by post title, then order by 2 meta keys – alphabetically. The posts come from custom post I’ve set up called “plant-catalogue”.

    So not this:
    Title
    – B (Key 0)
    – A (Key 1)

    This:
    Title
    – A (Key 0)
    – B (Key 1)

    <?php 
    
    $querystr = " SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta AS eventdate ON(
    $wpdb->posts.ID = eventdate.post_id
    AND eventdate.meta_key = 'Species'
    )
    LEFT JOIN $wpdb->postmeta AS eventtime ON(
    $wpdb->posts.ID = eventtime.post_id
    AND eventtime.meta_key = 'Variety'
    )
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->term_taxonomy.term_id = 3
    AND $wpdb->term_taxonomy.taxonomy = 'plant-catalogue'
    AND $wpdb->posts.post_status = 'publish'
    ORDER BY eventdate.meta_value, eventtime.meta_value ASC";
    
    ?>
    Thread Starter bradical911

    (@bradical911)

    thank you bcworkz that helps a lot in my understanding of the situation, i was starting to try and work out a solution with WP_Query but seems I’m wasting my time?

    Some untested “guess” code that i thought might need work but eventually work…

    $args = array(
    	'post_type'  => 'plant-catalogue',
    	'meta_query' => array(
    		array(
    			'key'    => 'Species',
    			'key'    => 'Variety',
    			'order'  => 'ASC',
    		),
    	),
    	'orderby'    => 'title meta_query',
    	'order'      => 'ASC',
    );
    $query = new WP_Query( $args );
    Thread Starter bradical911

    (@bradical911)

    Is it possible to do something here with meta_query ?

    Thread Starter bradical911

    (@bradical911)

    Thread Starter bradical911

    (@bradical911)

    Hi BCWorks,

    Thank for the reply, so its ANDing the search times, ok that makes sense.

    As I cant predict exactly what a user is going to type it only really works if it is doing “Ors” otherwise it fails as a search which makes it pretty useless especially when the words themselves are within the website maybe just not in the same order in the same place (could be anywhere in title, custom meta, content), just not that “exact phrase”.

    Do you know if there any plugins that get round this or can the search form itself be altered or functions.php have anything added.

    Funnily enough when I search for answers on this i don’t get much luck either.

    FYI my search form:

    <form id="search_form" action="http://www.champernowne.co.uk/beta/" method="get">
    
    			<input class="search" type="text" name="s" value="search..." onblur="if (this.value == '') {this.value = 'search...';}" onfocus="if (this.value == 'search...') {this.value = '';}" />
    
    			<input type="hidden" name="post_type" value="plant-catalogue" />
    
    			<input class="search-button" id="searchsubmit" type="submit" alt="Search" value="GO!" />
    
    		</form>
    Thread Starter bradical911

    (@bradical911)

    if it helps get a few responses here is my code for search.php – nothing particularly exciting…

    <?php get_header(); ?>
    
    <section>
    
    <h1>Results for - "<?php the_search_query(); ?>"</h1>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <article id="post-<?php the_ID(); ?>">
    
    <h2 class="page-title" itemprop="headline"><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h2>
    
    <?php
    	if (has_post_thumbnail() ) { // show small pic
    		//echo "small pic";
    		echo "<div class=\"thumbnail-post thumbnail-small\">";
    		the_post_thumbnail( 'small' );
    		echo "</div>";
    	} else {}
    ?>
    
    <?php echo the_meta(); ?>
    
    <p>▸ <a href="<?php echo get_permalink(); ?>">Read More</a></p>
    
    <?php endwhile; else : ?>
    
    	<h1><?php _e( 'Page Not Found!'); ?></h1>
    
    </article>
    
    <?php endif; ?>
    
            </section>
    
            <aside>
    		<?php get_sidebar(); ?>
            </aside>
    
    <?php get_footer(); ?>
    Thread Starter bradical911

    (@bradical911)

    A friend offered this solution which works nicely. However he points out that:

    The problem with that is there will be a load of unnecessary looping. Say you get 1000 rows back from the database but it only actually outputs 2.

    For me it works fine on a small site but would be interested in anything that can reduce querys to the DB.

    <ul>
            <?php
              /**
               * Create an array to hold all of the post titles
               */
              $post_titles = array();
    
              $lastposts = get_posts('&post_type=catalogue&orderby=title&order=ASC&numberposts=-1');
    
              foreach($lastposts as $post) :
    
                $post_title = get_the_title();
    
                if(!in_array($post_title, $post_titles)) :
    
                  /**
                   * Add the post title to the array
                   */
                   array_push($post_titles, $post_title);
                   setup_postdata($post);
                   // somehow remove duplicates titles?
            ?>
              <li>
                <a href="?s=<?php the_title(); ?>&post_type=catalogue">
                  <?php the_title(); ?>
                </a>
              </li>
            <?php
                endif;
              endforeach;
            ?>
            </ul>
Viewing 9 replies - 1 through 9 (of 9 total)