Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to create an index page for some custom fields, just like the taxonomy search pages.

E.g. I have a custom field with slug=wpcf-delivery. I want an index page that lists all values where the meta value of wpcf-delivery is 1.

I've tried creating a link like this: myblog.com/mkey=wpcf-delivery&mval=1 Then inserted the following code:

function eik_custom_fields_search( $query ) {
    if ( !is_admin() && $_GET['mkey'] ) {
        $query->meta_query = true;
        $query->set('meta_query', array(
           array(
                'key' => $_GET['mkey'],
                'value' => $_GET['mval'],
                'compare' => '=',
            )
        ));
    };
}
add_filter( 'pre_get_posts', 'eik_custom_fields_search');

But it returns no results.

Any ideas?

The results of print_r($query):

WP_Query Object (
    [query_vars] => Array (
        [error] =>
        [m] => 0
        [p] => 0
        [post_parent] =>
        [subpost] =>
        [subpost_id] =>
        [attachment] =>
        [attachment_id] => 0
        [name] =>
        [static] =>
        [pagename] =>
        [page_id] => 2
        [second] =>
        [minute] =>
        [hour] =>
        [day] => 0
        [monthnum] => 0
        [year] => 0
        [w] => 0
        [category_name] =>
        [tag] =>
        [cat] =>
        [tag_id] =>
        [author_name] =>
        [feed] =>
        [tb] =>
        [paged] => 0
        [comments_popup] =>
        [meta_key] =>
        [meta_value] =>
        [preview] =>
        [s] =>
        [sentence] =>
        [fields] =>
        [menu_order] =>
        [category__in] => Array ( )
        [category__not_in] => Array ( )
        [category__and] => Array ( )
        [post__in] => Array ( )
        [post__not_in] => Array ( )
        [tag__in] => Array ( )
        [tag__not_in] => Array ( )
        [tag__and] => Array ( )
        [tag_slug__in] => Array ( )
        [tag_slug__and] => Array ( )
        [meta_query] => Array (
            [0] => Array (
                [key] => wpcf-service-speed
                [value] => 2
                [compare] => =
            )
        )
    )
    [tax_query] => WP_Tax_Query Object (
        [queries] => Array ( )
    [relation] => AND
    )
    [meta_query] => 1
    [post_count] => 0
    [current_post] => -1
    [in_the_loop] =>
    [comment_count] => 0
    [current_comment] => -1
    [found_posts] => 0
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] =>
    [is_preview] =>
    [is_page] => 1
    [is_archive] =>
    [is_date] =>
    [is_year] =>
    [is_month] =>
    [is_day] =>
    [is_time] =>
    [is_author] =>
    [is_category] =>
    [is_tag] =>
    [is_tax] =>
    [is_search] =>
    [is_feed] =>
    [is_comment_feed] =>
    [is_trackback] =>
    [is_home] =>
    [is_404] =>
    [is_comments_popup] =>
    [is_paged] =>
    [is_admin] =>
    [is_attachment] =>
    [is_singular] => 1
    [is_robots] =>
    [is_posts_page] =>
    [is_post_type_archive] =>
    [query_vars_hash] => 4e01d4a2ac7ac063e19e57f91931e95c
    [query_vars_changed] =>
    [thumbnails_cached] =>
    [query] => Array ( )
)
share|improve this question
    
Why don't you use custom taxonomy instead of custom fields (is there any reason)? Could you show, what SQL query does your modified wp_query produce (just print_r the $query variable in your filter function)? –  Krzysiek Dróżdż Jul 1 '13 at 20:27
    
The reason is mainly because my client already has a load of data entered as custom fields. I'll add the print_r results to the question –  adamf321 Jul 1 '13 at 20:46
    
you have an extra comma in there, not sure that is the issue tough. Try removing the comma after the compare Also see this question: stackoverflow.com/questions/15613750/… –  BandonRandon Jul 1 '13 at 20:57
    
You can have trailing commas in PHP arrays, it shouldn't matter. –  t31os Jul 2 '13 at 18:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.