Support

Account

Home Forums General Issues Add URL parameter from ACF to CPT post

Solved

Add URL parameter from ACF to CPT post

  • I want to add an url parameter to all links in wordpress, which linked to my custom post type post. The url parameter should come from an advanced custom text field.

    custom post type = “fahrten”
    the advanced custom text field = “fahrt_id”
    current url = http://example.com/fahrten/kanal-rundfahrt
    desired url = http://example.com/fahrten/kanal-rundfahrt?tour_id=xxx (xxx = value of acf “fahrt_id”)

    I’ve tried this (https://www.advancedcustomfields.com/resources/query-posts-custom-fields/) and saved the permalinks.

    
    function my_pre_get_posts( $query ) {
    	
    	// do not modify queries in the admin
    	if( is_admin() ) {
    		
    		return $query;
    		
    	}
    	
    	
    	// only modify queries for 'event' post type
    	// if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'fahrten' ) {
    	
    	if( isset( $query->query_vars['post_type']) && $query->query_vars['post_type'] == 'fahrten') {
    	
    		// allow the url to alter the query
    		if( isset($_GET['fahrt_id']) ) {
    			
    			// $query->set('meta_key', 'fahrt_id');
    			// $query->set('meta_value', $_GET['fahrt_id']);
    			$query->set('meta_query', array(
    				'key' => 'fahrt_id',
    				'value' => $_GET['fahrt_id'],
    				'compare' => 'LIKE'
    			)
    		);
    		
    		}
    	
    	}
    	
    	
    	// return
    	return $query;
    	
    }
    
    add_action('pre_get_posts', 'my_pre_get_posts');

    But nothing happened.

    • vverner

    • August 14, 2019 at 10:52 pm

    Hey man!

    Sorry if I did not understand your question correctly.

    But the “add_action” you are using in this code is only for the posts query (like filtering CPTS when they appear on your website frontend).

    To update permalinks you need some +/- like this:

    function vv_cpt_acf_permalink($post_link, $post = 0)
    {
    	if ($post->post_type === 'fahrten') {
                    $post_id = {query_of_post_by_slug};
                    $field_value = get_field("fahrt_id",$post_id);
    		return $post_link . '/?tour_id='.$field_value);
    	} else {
    		return $post_link;
    	}
    }
    add_filter('post_type_link', 'vv_cpt_acf_permalink', 1, 3);
  • @vverner

    That seems to be exactly what I was looking for!

    Can you explain what you mean with: {query_of_post_by_slug}?

    • vverner

    • August 14, 2019 at 11:26 pm

    Awesome!

    I guess this could work (not tested)

    Change the ” {query_of_post_by_slug} ” for get_the_ID($post);

    function vv_cpt_acf_permalink($post_link, $post = 0)
    {
    	if ($post->post_type === 'fahrten') {
                    $post_id = get_the_ID($post);
                    $field_value = get_field("fahrt_id",$post_id);
    		return $post_link . '/?tour_id='.$field_value);
    	} else {
    		return $post_link;
    	}
    }
    add_filter('post_type_link', 'vv_cpt_acf_permalink', 1, 3);
    • vverner

    • August 14, 2019 at 11:27 pm

    Remember to save your permalink settings after use this function

  • OMG. You are my hero. This works fine and is exactly what I was looking for! Many thanks. Have been searching for a solution for days.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.