• Resolved Marco1970

    (@marco1970)


    Hi Mikko,

    hope all is well with you.

    I need to serialize the WP Query at the end of the Woo loop, and then un-serialize it at a later stage to re-run it and process the results in a different context / page.

    Unfortunately, the un-serialized query returns an empty result set when Relevanssi is enabled. If I disable your plugin, the search results are the same between the original search and un-serialized one.

    To give you an hint (if I may), the same happened with the BeRocket Advanced Ajax filters, which when enabled caused the search results to be different between the original and the un-serialized search. The solution was to add two custom properties to the WP Query object before serializing it:

    global $wp_query;
    $buffer_query = $wp_query->query_vars;
    $buffer_query[‘bapf_apply’] = true; // BeRocket Custom property #1
    $buffer_query[‘bapf_save_query’] = true; // BeRocket Custom property #2
    $ser = serialize($buffer_query);
    ….

    I therefore wonder if I need to do the same for Relevanssi (or any processing of the WP Query before serializing it) in order to ensure the correct results.

    Thanks in advance for your help!

    M

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Marco1970

    (@marco1970)

    >> “….I therefore wonder if I need to do the same for Relevanssi (or any processing of the WP Query before serializing it) in order to ensure the correct results”

    …or any post-processing of the un-serialized query before running it…

    whatever it takes ??

    TY

    M

    Plugin Author Mikko Saari

    (@msaari)

    Relevanssi works by hooking on to the posts_pre_query hook. Relevanssi runs the search and places the posts found in the $query->posts attribute and sets $query->max_num_posts and $query->found_posts.

    If you serialize the WP_Query object after that happens and when these attributes are set, I don’t know why the parameters wouldn’t remain as set. I added

    $ser = serialize( $wp_query );

    after the endwhile on my theme search results template, and the resulting serialized object has the correct results in the posts attribute and all the rest. I could then unserialize the query object and loop through it without problems.

    Relevanssi doesn’t do anything that should require any extra steps here. This should just work, as far as I can tell, but I don’t know what exactly are you doing here.

    Thread Starter Marco1970

    (@marco1970)

    Hi Mikko, thanks for quick reply, truly appreciate it.

    sorry, my bad, I described poorly:

    1) I serialize the WP Query vars, NOT the query itself nor the results:

    global $wp_query;
    $buffer_query = $wp_query->query_vars;
    $buffer_query[‘fields’] = ‘ids’;
    $buffer_query[‘nopaging’] = true;
    $buffer_query[‘bapf_apply’] = true;
    $buffer_query[‘bapf_save_query’] = true;
    $ser = serialize($buffer_query);

    Reason is that the results are paginated, max 25 per page, and when I need to rerun the query later I need the whole result set.

    2) I unserialize as follows:

    $buffer_query = unserialize($ser);
    $query = new WP_Query($buffer_query);
    $results = $query->posts;

    3) Based on what you said in your kind reply, my guess is that when a WP Query is re-created from serialized vars, the Relevannsi ‘posts_pre_query hook’ is not called.

    any way I can do that programmatically?

    TY

    M

    Plugin Author Mikko Saari

    (@msaari)

    Ok, in that case it should be easy: just set relevanssi to true:

    $buffer_query['relevanssi'] = true;

    • This reply was modified 2 years, 4 months ago by Mikko Saari.
    Thread Starter Marco1970

    (@marco1970)

    Hi Mikko,

    yes, that worked, thanks!!

    M

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Serialize / unserialize query’ is closed to new replies.