Title: pre_get_posts problem
Last modified: August 19, 2016

---

# pre_get_posts problem

 *  Resolved [grosbouff](https://wordpress.org/support/users/grosbouff/)
 * (@grosbouff)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/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;
   
       	}
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Reuben](https://wordpress.org/support/users/greuben/)
 * (@greuben)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/pre_get_posts-problem/#post-1952649)
 * 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.wordpress.org/ticket/14645](http://core.trac.wordpress.org/ticket/14645)
   for more information.
 * Edit:
    **Try** `$query->set( 'meta_query', array( array( 'key' => 'opq_guest_email','
   value' => $dummy_email, 'type' => 'string', 'compare' => '=' ) ) );`
 *  Thread Starter [grosbouff](https://wordpress.org/support/users/grosbouff/)
 * (@grosbouff)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/pre_get_posts-problem/#post-1952656)
 * except for the typo, it works ! thanks !
 * `$query->set( 'meta_query', array( array( 'key' => 'oqp_guest_email', 'value'
   => $dummy_email, 'type' => 'string', 'compare' => '=' ) ) );`
 *  [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/pre_get_posts-problem/#post-1952819)
 * [http://core.trac.wordpress.org/ticket/16742](http://core.trac.wordpress.org/ticket/16742)
 *  [anmari](https://wordpress.org/support/users/anmari/)
 * (@anmari)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/pre_get_posts-problem/#post-1952971)
 * 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 ?
 *  [anmari](https://wordpress.org/support/users/anmari/)
 * (@anmari)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/pre_get_posts-problem/#post-1952972)
 * 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.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘pre_get_posts problem’ is closed to new replies.

## Tags

 * [meta_key](https://wordpress.org/support/topic-tag/meta_key/)
 * [pre_get_posts](https://wordpress.org/support/topic-tag/pre_get_posts/)

 * 5 replies
 * 4 participants
 * Last reply from: [anmari](https://wordpress.org/support/users/anmari/)
 * Last activity: [15 years, 2 months ago](https://wordpress.org/support/topic/pre_get_posts-problem/#post-1952972)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
