Posts per page

  • HaikoG
    Participant
    # 4 weeks, 1 day ago

    Hi,

    I’m working on a way to get a posts-per-page function working through a querystring. My theme has a build in feature where one can select 12/24 products per page, so I tried to catch that with this code.
    Unfortunately it doesn’t work. set_nb_results_by_page() does not seem to change the actual value, it stays on the default.

    Any chance you can assist?

    [code]
    add_filter( WPSOLR_Events::WPSOLR_FILTER_UPDATE_WPSOLR_QUERY, ‘hg_solr_query’, 99, 1);
    function hg_solr_query(WPSOLR_Query $wpsolr_query){
    $current_query = $wpsolr_query->get_filter_query_fields();

    if (isset($_GET[‘products-per-page’])){
    $wpsolr_query->set_nb_results_by_page($_GET[‘products-per-page’]);
    error_log(‘found query var: ‘.$_GET[‘products-per-page’]);
    }else{
    $wpsolr_query->set_nb_results_by_page(“24”);
    error_log(‘Did not find query var, setting 24’);
    }

    error_log(‘results per page:’);
    error_log($wpsolr_query->get_nb_results_by_page());

    $wpsolr_query->set_filter_query_fields($current_query);

    return $wpsolr_query;

    }
    [/code]

    HaikoG
    Participant
    # 4 weeks, 1 day ago

    I figured it out.. The getter and the setter aren’t named equal

    $wpsolr_query->get_nb_results_by_page() gets the result
    $wpsolr_query->wpsolr_set_nb_results_by_page() sets the result. notice the wpsolr in the functionname?

    For consistency it would be nice to have these kind of functions named similar, but I caught it for this one 🙂

    wpsolr
    Keymaster
    # 4 weeks ago

    You’re correct, bur for compatibility reasons the new setter name prefix was not back-ported to the getter.

    Anyway, those functions are not meant to be used but by WPSOLR code, and could be modified in the future.

    You could use instead the method search_engine_client_set_rows() inside the hook WPSOLR_Events::WPSOLR_ACTION_SOLARIUM_QUERY

    Extracts from wp-content/plugins/wpsolr-pro/wpsolr/pro/extensions/theme_directory2/class-wpsolr-theme-directory2.php:

    add_action( WPSOLR_Events::WPSOLR_ACTION_SOLARIUM_QUERY, [
    	$this,
    	'wpsolr_action_query',
    ], 10, 1 );
    
    public function wpsolr_action_query( $parameters ) {
    
     /* @var WPSOLR_AbstractSearchClient $search_engine_client */
     $search_engine_client = $parameters[ WPSOLR_Events::WPSOLR_ACTION_SOLARIUM_QUERY__PARAM_SOLARIUM_CLIENT ];
    
     $search_engine_client->search_engine_client_set_rows( 20 );
    }
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.