WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] pre_get_posts problem (6 posts)

  1. grosbouff
    Member
    Posted 3 years ago #

    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;
    
    	}
  2. Reuben Gunday
    Member
    Posted 3 years ago #

    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 for more information.

    Edit:
    Try
    $query->set( 'meta_query', array( array( 'key' => 'opq_guest_email', 'value' => $dummy_email, 'type' => 'string', 'compare' => '=' ) ) );

  3. grosbouff
    Member
    Posted 3 years ago #

    except for the typo, it works ! thanks !

    $query->set( 'meta_query', array( array( 'key' => 'oqp_guest_email', 'value' => $dummy_email, 'type' => 'string', 'compare' => '=' ) ) );

  4. scribu
    Member
    Posted 3 years ago #

  5. anmari
    Member
    Posted 3 years ago #

    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 ?

  6. anmari
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic