0

I have category 'news' http://localhost/foldername/news, for each 'news' posts I have custom field called 'years' and is set up as a Text Field. I have 2 values assigned n different posts '2013' and '2014'.

What I'm trying to do first to show only 2014 posts in 'news' category http://localhost/foldername/news and second to pass url parameter so if I want to show '2013' to use the following http://localhost/foldername/news/?years=2013

I have selected Attribute so user will select which year posts to show but as default will show 2014 posts

 <select>
  <option value="2014">2014</option>
  <option value="2013">2013</option>
</select>

For the first I found the following solution to show only '2014' posts in 'news' http://localhost/foldername/news:

    $args= array(   
    'meta_key' => 'years',
    'meta_value' => '2014',
    );
   query_posts($args);

Now my big problem is that I pass the parameter to show '2013' posts but is not working

Tried something like this:

function wpse129223_custom_meta_query( $query ) {

    // If not on the main query, or on an admin page, bail here
    if ( ! $query->is_main_query() || is_admin() ) {
    return;
}

// If 'type' isn't being passed as a query string, bail here
if ( empty( $_GET['years'] ) ) {
    return;
}

// Get the existing meta query, cast as array
$meta_query = (array) $query->get('meta_query');

    // Convert 'type' into an array of types
    $types = explode( ',', $_GET['years'] );

    // Ensure that types aren't empty
    if ( is_array( $types ) && ! empty( $types ) ) {

        // Build a meta query for this type
        $meta_query[] = array(
            'key'     => 'years',
            'value'   => $types,
            'compare' => 'IN',
        );

        // Update the meta query
        $query->set( 'meta_query', $meta_query );
    } 

}
add_action( 'pre_get_posts', 'wpse129223_custom_meta_query' );

But every time I go to http://localhost/foldername/news/?years=2013 still shows me '2014' posts

4
  • Have you still got the query_posts($args); running? If so remove that. Then in your pre_get_posts do a check for is_category( 'news' ) at the top. Then set the default years to "2014" by removing the check on empty( $_GET... and using $types = explode( ',', empty( $_GET['years'] ) ? "2014" : $_GET['years'] );
    – bonger
    Commented Jul 31, 2015 at 4:51
  • @bonger Please add your comment as an answer. This is useful info that will just rot and die in comments, it deserves to be in an answer ;-). Commented Jul 31, 2015 at 5:47
  • @bonger Agree with Pieter Goosen, will appreciate if you post it as an answer Commented Jul 31, 2015 at 8:07
  • Wilco.........!
    – bonger
    Commented Jul 31, 2015 at 15:50

1 Answer 1

1

Per comments, just use pre_get_posts filter (no query_posts()) very much as you have it with a few adjustments, eg test for $query->is_category( 'news' ) so that it only runs on news queries, and set the default to "2014" rather than bailing on no $_GET, eg

function wpse129223_custom_meta_query( $query ) {

    // If not on the main query, or on an admin page, bail here
    if ( ! $query->is_main_query() || is_admin() || ! $query->is_category( 'news' ) ) {
        return;
    }

    // Get the existing meta query, cast as array
    $meta_query = (array) $query->get('meta_query');

    // Convert 'type' into an array of types
    $types = explode( ',', empty( $_GET['years'] ) ? '2014' : $_GET['years'] );

    // Ensure that types aren't empty
    if ( is_array( $types ) && ! empty( $types ) ) {

        // Build a meta query for this type
        $meta_query[] = array(
            'key'     => 'years',
            'value'   => $types,
            'compare' => 'IN',
        );

        // Update the meta query
        $query->set( 'meta_query', $meta_query );
    } 

}
add_action( 'pre_get_posts', 'wpse129223_custom_meta_query' );
2
  • Thank you so much!!!!!!!!!!!!!!!! Also If I want to add more than one category to show only 2014 what I need to change tried something like this but didnt work $query->is_category( 'news' , 'liveleak' ) Commented Jul 31, 2015 at 16:16
  • Good stuff! If you put them in an array it should work, $query->is_category( array( 'news' , 'livelea' ) )
    – bonger
    Commented Jul 31, 2015 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.