WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] rewriting & wp_redirect problem (2 posts)

  1. grosbouff
    Member
    Posted 2 years ago #

    Hi, i'm currently developping a classified ads plugin.

    For this; i created a custom post type (ads) and 3 main taxonomies (ad_type,ad_category,ad_tag).

    Let's say we are looking for a red(ad_tag) car(ad_category) in the offers(ad_type).

    When you are browsing the ads and viewing the results, you can save the "search filters" used.

    It will save a user meta with the subscription (search filters) parameters; eg.

    umeta_id	user_id		meta_key	meta_value
       147		   1	   'ad_subscription'	array('ad_type'=>'offer','ad_category'=>'cars','ad_tag'=>'red');

    On the main ads page; a link for the user will be available; eg. /wordpress/ads/my-subscriptions/147.
    If I click on that link; it will load the subscription #147; fill the wp_query arguments with it;and the page will load the matching ads and display them in a custom way (because it's a subscription page).

    Here's my problem.
    Let's say we saved this subscription.

    But now, what if we are not on the subscription page but that we are searching for a red car in the ad offers...
    It is exactly the same arguments than in the subscription we saved.

    So I wrote a function to compare the searched arguments with every subscription's arguments; so I can now if the search made already have been saved in a subscription.
    That's one step.

    If the searched arguments ARE already saved in a subscription; I would like to REDIRECT to that subscription.
    And here i'm stuck. It makes an infinite loop.

    Could you see something that's wrong in this ? :

    add_action('pre_get_posts','ads_redirect_to_subscription');
    
    function ads_redirect_to_subscription(&$query) {
    
    	if ($query->query_vars['post_type']!='ad') return $query; //we are not searching for ads
    	if ($query->query_vars['ads_subscription']) return $query; //we ARE loading a subscription
    
    	$search = yclads_filter_query_args($query); //get ad search arguments
    
    	if(!$search) return $query; //no ad search arguments found
    
    		$subscription_id=self::args_are_already_subscription_id($search); //compare search arguments with subscriptions
    
    		if ($subscription_id!=$query->query_vars['ads_subscription']){
    			wp_redirect(yclads_get_subscription_link($subscription_id));
    			die();
    
    		}
    	}

    Thanks a lot !

  2. grosbouff
    Member
    Posted 2 years ago #

    I finally found out.
    Don't think anyone will need this, but it can help :

    function redirect_to_subscription(&$query) {
    		global $wp_query;
    
    		//!!WE NEED TO USE WP_QUERY HERE
    
    		if ($wp_query->query_vars['post_type']!='yclad') return $query;
    		//if ($query->query_vars['yclads_subscription']) return $query;
    
    		//SUBSCRIPTION QUERY
    		$q_subscription_id=yclads_get_subscription_id_from_slug($wp_query->get('yclads_subscription'));
    
    		//SEARCHED ARGS
    		$search = yclads_filter_query_args($wp_query);
    		if(!$search) return $query;
    		$u_subscription_id=self::args_are_already_subscription_id($search);
    
    		if($q_subscription_id==$u_subscription_id) return $query;
    
    		$existing_subscription=$this->user_subscriptions[$u_subscription_id];
    
    		wp_redirect(yclads_get_subscription_link($existing_subscription['slug']));die();
    	}

Topic Closed

This topic has been closed to new replies.

About this Topic