Posts per page

  • HaikoG
    Participant
    4 years, 4 months ago #15955

    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 years, 4 months ago #15956

    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 years, 4 months ago #15959

    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 );
    }
    mneimne
    Participant
    1 year, 11 months ago #29708

    Bumping an old thread, but thought this might benefit someone.

    Here is how I did it
    `if ( defined( ‘WPSOLR_PLUGIN_SHORT_NAME’ ) ) {
    add_action( WPSOLR_Events::WPSOLR_ACTION_SOLARIUM_QUERY, function () {
    //default per page 100
    $per_page = 100;
    //read from URL string
    if(isset($_GET[‘per_page’]) && is_int((int)$_GET[‘per_page’]))
    $per_page = $_GET[‘per_page’];

    $search_engine_client = WPSOLR_Service_Container::get_solr_client();
    $query_object = WPSOLR_Service_Container::get_query();
    $query_object->wpsolr_set_nb_results_by_page($per_page);
    $search_engine_client->search_engine_client_set_rows( $per_page );
    }
    } );
    }

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.