WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] pre_get_posts question (2 posts)

  1. henri
    Member
    Posted 3 years ago #

    Hi I want to filter query only for one category.

    My original filter was :

    add_action('pre_get_posts', 'remove_old_apple');
    function remove_old_apple(&$query)
    {
      	if (is_category()&& empty( $query->query_vars['suppress_filters'] ) ) {
            $today = date("m.d.y");
            global $custom_remove_old_apple;
           	$query->set('meta_key', 'date_maj');
            $query->set('meta_value', $today);
            $query->set('meta_compare', '=');
            }
    }

    And work for all category page, but I want to filter only on the category ID 39 so I try this

    add_action('pre_get_posts', 'remove_old_apple');
    function remove_old_apple(&$query)
    {
     	if (is_category('39')&& empty( $query->query_vars['suppress_filters'] ) ) {
    
            $today = date("m.d.y");
            global $custom_remove_old_apple;
           	$query->set('meta_key', 'date_maj');
            $query->set('meta_value', $today);
            $query->set('meta_compare', '=');
            }
    }

    Why out any success. Do you have some suggestions?
    Thanks for your help

  2. henri
    Member
    Posted 3 years ago #

    Found this solution:

    if (($query->query_vars['category_name'] == 'achat-macbook')&& empty( $query->query_vars['suppress_filters'] ) ) {

    I think that you can use the cat ID instead the category name for this hack.

Topic Closed

This topic has been closed to new replies.

About this Topic