pre_get_posts problem
-
Hi, I made a plugin to allow non-logged visitors to posts from the frontend.
It uses a specific user as “guest account”.
When a guest post something, a meta_key “oqp_gkey” is saved with the post; containing this visitor email (which he filled with the form).Now; when I display the posts by author (eg. author/guest); it lists all posts by the “guest” account, not only the posts by this particular guest.
So I added a parameter to the author URL (eg. author/guest/?oqp_gkey=CRYPTED_EMAIL) which should retrieve the posts written only by this “email”.
For this, I use pre_get_posts.
But even if it seems that my query IS set with the values I want; the posts returned do not match the meta_key; it returns all posts by the “guest” account.
Any idea ?Here’s the code :
add_filter('pre_get_posts',array(&$this,'filter_query')); function filter_query(&$query) { if ($query->query_vars['suppress_filters']) return $query; print_r("add guest query filter"); //DO PRINT $dummy = $this->dummy; //GET DUMMY USER $wp_author = $query->get('author_name'); //QUERY IS FILTERED BY AUTHOR NAME $wp_author_ID = $query->get('author'); //QUERY IS FILTERED BY AUTHOUR ID if (($wp_author !=$dummy->user_nicename) && (!oqp_user_is_dummy($wp_author_ID)))return false; //author is not the dummy user if ($wp_author==$dummy->user_nicename) { $dummy_email=$query->get('oqp_gkey'); $dummy_email = oqp_simple_decode($dummy_email,'oqp-dummy-email'); //DECRYPT EMAIL }elseif((oqp_user_is_dummy($wp_author_ID)) && ($post)) { $dummy_email = get_post_meta($post->ID,'oqp_guest_email', true); } if (!$dummy_email) return false; //no key to analyze print_r("do guest query filter: ".$dummy_email); //DO PRINT //get all ads for this email, //not only for this guest $query->set('author_name',false); $query->set('author',false); $query->set('meta_key','oqp_guest_email'); $query->set('meta_value',$dummy_email); add_action('oqp_after_the_loop',array(&$this,'remove_query_filter')); return $query; }
-
Change this to
$query->set('meta_key','oqp_guest_email'); $query->set('meta_value',$dummy_email);to
$query->set( 'meta_query', array( 'key' => 'opq_guest_email', 'value' => $dummy_email, 'type' => 'string', 'compare' => '=' ) );See this ticket http://core.trac.ww.wp.xz.cn/ticket/14645 for more information.
Edit:
Try
$query->set( 'meta_query', array( array( 'key' => 'opq_guest_email', 'value' => $dummy_email, 'type' => 'string', 'compare' => '=' ) ) );except for the typo, it works ! thanks !
$query->set( 'meta_query', array( array( 'key' => 'oqp_guest_email', 'value' => $dummy_email, 'type' => 'string', 'compare' => '=' ) ) );Hi scribu,
am I understanding the ticket correctly?
In 3.1 and above I have
$metaq = ( array (array( 'key' => '_VEVENT', 'value' => '1', 'type' => 'string', 'compare' => '=' ) ) ); $query->set('meta-query', $metaq );and it does not work – all posts shown.
for < 3.1 I have the
`$query->set(‘meta_key’, ‘_VEVENT’);
$query->set(‘meta_value’, ‘1’);`and that works.
If I use the meta_key (< 3.1 code) in 3.1, that also does not work – all posts shown again.
So we have lost functionality in 3.1 ? for now ?
I should add that this is when trying to modify the query that list events in the edit posts menu, using ‘pre_get_posts’ filter.
I am pretty sure that the meta query was/is working in when generating own custom query of events, and also weird that it works in lower version.
Not a train smash for me in my plugin as people also have option to use custom post types as events or to allocate a category as the “event” category and of course those all work nicely.
I am just trying to satisfy someone who wanted standard posts as events BUT NOT limited to a particular category.
The topic ‘pre_get_posts problem’ is closed to new replies.