Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom post type, query on title, then display eash post. WordPress

  • SOLVED

I had asked a smiliar question here but feel instead of expanding on that and to make it fair for the Experts, I will open a new question.

I have a custom post type, memberlist.
Also have meta fields of member_name, member_phone, etc.

I want to search and display the posts with the following criteria:
To display the post where the title starts with and alaphabet letter. Using 'A', would return and display all post that has a title that starts with A, and 'B' would return and display all post that has a title that starts with B. It would also display the meta fields to( ie; using something like `$member_telephone = $custom["member_telephone"][0];` ).

Answers (4)

2010-10-21

Utkarsh Kukreti answers:

$posts = $wpdb->get_results( "
SELECT *
FROM $wpdb->posts AS p
WHERE p.post_type = 'memberlist'
AND LEFT( p.post_title, 1 ) = 'A'
" );


Then loop using a foreach

foreach( $posts as $post )
{
$custom = get_post_custom($post->ID);
}

2010-10-22

rilwis answers:

You can use this:

global $wpdb;

$my_posts = $wpdb->get_results( "
SELECT *
FROM $wpdb->posts AS p
WHERE p.post_type = 'memberlist'
AND LEFT( p.post_title, 1 ) = 'a'
" );

$backup = $post;

foreach( (array) $my_posts as $post ) {
setup_postdata( $post );

// show post data, using Template tags
the_title();
the_content();

// show post meta data
$telephone = get_post_meta( $post->ID, 'member_telephone', true );

if( !empty( $telephone ) ) {
echo $telephone;
}
}
$post = $backup;

2010-10-21

Cosmin Popovici answers:

For the search part, you could use [[LINK href="http://wordpress.org/extend/plugins/wp-custom-fields-search/"]]WP Custom Fields Search[[/LINK]]

Then, to display results based on a custom field, check [[LINK href="http://www.designjuices.co.uk/2010/06/tutorial-wordpress-custom-post-types/"]]this[[/LINK]] and [[LINK href="http://pixeljar.net/2009/01/13/order-posts-by-custom-key-revisited/"]]this[[/LINK]] out ;)

Furthermore, if your results page loops a div, you can [[LINK href="http://efreedom.com/Question/1-2501789/JQuery-Sort-Divs-According-Content-Different-Sub-Divs"]]sort the divs with this cool jQuery script[[/LINK]]

2010-10-21

John Cotton answers:

$wpdb->get_results with the complete SQL is one option, but risks problems with database changes in future versions.

A better option might be to use the [[LINK href="http://codex.wordpress.org/Custom_Queries"]]custom queries built in to WP[[/LINK]].

That would yield code something like this:




add_filter('posts_where', 'byInitialLetter' );
function byInitialLetter( $where )
{
$where = preg_replace(
"/\(\s*post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"post_title LIKE '$YOUR_LETTER%'", $where );
}

return $where;
}

query_posts(array(
'post_type' => 'directory',
'post_status' => 'publish',
'order' => 'ASC'
));

if ( have_posts() ) :
while ( have_posts() ) : the_post();
$custom_fields = get_post_custom($post->ID);
// etc etc using $custom_fields to access your custom values for output/code flow