• I added a select-form in admin/edit.php cause i want to filter the list to only show posts with specific meta_value.

    This is what i added/want to add:

    <?php // view filters
    if ( !is_singular() ) {
    $carc_query = "SELECT DISTINCT YEAR(meta_value) AS yyear, MONTH(meta_value) AS mmonth FROM $wpdb->postmeta WHERE meta_key = '_EventEndDate' ORDER BY meta_value DESC";
    
    $carc_result = $wpdb->get_results( $carc_query );
    $month_count = count($carc_result);
    
    if ( $month_count && !( 1 == $month_count && 0 == $carc_result[0]->mmonth ) ) {
    $postmeta = isset($_GET['postmeta']) ? (int)$_GET['postmeta'] : 0;
    
    ?>
    <select name='postmeta'  id='postmeta' class='postform'>
    <option<?php selected( $m, 0 ); ?> value='0'>Kalender</option>
    <?php
    foreach ($carc_result as $carc_row) {
    	if ( $carc_row->yyear == 0 )
    		continue;
    	$carc_row->mmonth = zeroise( $carc_row->mmonth, 2 );
    
    	if ( $carc_row->yyear . $carc_row->mmonth == $m )
    		$default = ' selected="selected"';
    	else
    		$default = '';
    
    	echo "<option$default value='" . esc_attr("$arc_row->yyear$arc_row->mmonth") . "'>";
    	echo $wp_locale->get_month($carc_row->mmonth) . " $arc_row->yyear";
    	echo "</option>\n";
    }
    ?>
    </select>
    <?php }
    }
    ?>

    That works so faar, i get a nice dropdown with all month-dates (From module: The events Calendar) thats submitted in all the posts…

    But i have no clue on how to get the search results to use that filter…

    Can anyone help me?

Viewing 8 replies - 1 through 8 (of 8 total)
  • You can filter posts on the edit page without touching core, the pre_get_posts filter/action is appropriate for adding meta parameters to the query that grabs the posts. Same goes for your dropdown, no need to directly edit edit.php, i’ve done this before myself (without core hacking), so i’m certain it’s possible.

    Been a while since i last did this, but try something like..

    // Untested
    
    add_filter( 'pre_get_posts', 'meta_filter_posts' );
    function meta_filter_posts( $query ) {
    	if( is_admin() && is_search() && isset( $_GET['postmeta'] ) ) {
    		// $query is the WP_Query object, set is simply a method of the WP_Query class that sets a query var parameter
    		$query->set( 'meta_key', 'key_name' );
    		$query->set( 'meta_value', 'some_value' );
    	}
    	return $query;
    }

    For doing the query filtering part..

    I know i’ve done meta filtering on the edit page before, be damned if i can find a thread/post where i’ve given an example though..

    Any problems, post back, i can always figure it out again.. ??

    Thread Starter bryggfogden

    (@bryggfogden)

    Hello and thanks for answer. ill give this a shot and report back.

    Thread Starter bryggfogden

    (@bryggfogden)

    tried this some now, seems im able to get the filter into the query, but i get 0 results even i copied the value from phpmyadmin so i know i should get atleast one hit..

    function meta_filter_posts( $query ) {
    	if( isset( $_GET['the_meta'] ) ) {
    		if ($_GET['the_meta'] > 0) {
    		// $query is the WP_Query object, set is simply a method of the WP_Query class that sets a query var parameter
    		$query->set( 'meta_key', '_EventStartDate' );
    		$query->set( 'meta_value', '2010-01-07 00:00:00' );
    	}
    	}
    	return $query;
    }
    add_filter( 'pre_get_posts', 'meta_filter_posts' );

    Works for me..

    Test code:

    //add_post_meta( 34, '_EventStartDate', '2010-01-07 00:00:00' );
    function meta_filter_posts( $query ) {
    	if( !is_admin() )
    		return $query;
    	if( isset( $_GET['the_meta'] ) ) {
    		if( $_GET['the_meta'] > 0 ) {
    		// $query is the WP_Query object, set is simply a method of the WP_Query class that sets a query var parameter
    		$query->set( 'meta_key', '_EventStartDate' );
    		$query->set( 'meta_value', '2010-01-07 00:00:00' );
    	}
    	}
    	return $query;
    }
    add_filter( 'pre_get_posts', 'meta_filter_posts' );

    The commented line was how i added the meta (quick way).

    Requested the page like so..
    example.com/wp-admin/edit.php?the_meta=1

    The only post then listed was the post i attached the meta to.

    Thread Starter bryggfogden

    (@bryggfogden)

    this works for me also… almost.

    $query->set( 'meta_key', '_EventStartDate' );
    $query->set( 'meta_value', '2010-01-07 00:00:00' );

    did not give me any results even tho the date is coped from phpmyadmin
    but if i changed to something like

    $query->set( 'meta_key', '_EventIsEvent' );
    $query->set( 'meta_value', 'yes' );

    I got results, so im guessing i cant query the date somehow…
    but also i guess i would want to query to do something like:
    WHERE meta_value LIKE '2010-01*'

    not shure how thou

    Unfortunately there’s no option for a like comparison using meta values/keys with query_posts (WP_Query), for LIKE queries on meta you’ll have to look at writing a seperate query to run prior to the main one, to sort out which items should be fed into the query.

    Basically anything listed here could be used inside the filter.
    https://codex.www.ads-software.com/Function_Reference/query_posts
    Parameters for custom fields (meta).
    https://codex.www.ads-software.com/Function_Reference/query_posts#Custom_Field_Parameters

    Had a filter working nicely using meta_keys previously. Upgraded to 3.1 and it’s broken (using the above examples)

    Anyone else having this issue?

    Nevermind… found your post over here Mark: https://www.ads-software.com/support/topic/parse_query-filter-changed-in-31 – thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘filter by postmeta/meta_value’ is closed to new replies.