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.

  • 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}?

  • 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);
  • 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.

  • hi
    I need help..
    I have post type = ‘team’
    My acf = ‘datestart’

    I add:

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

    and save permalink in WP and I don’t see the effect or I misunderstand this code…
    I would like to see in pos type “teams” browser example::

    https://domain.eu/teams/post-title?datestart=111111

  • my mistake. This code is ok

    function vv_cpt_acf_permalink($post_link, $post = 0)
    {
    	if ($post->post_type === 'team') {
                    $field_value = get_field("datestart",$datestart);
    		return $post_link . '/?datestart='.$field_value);
    	} else {
    		return $post_link;
    	}
    }
    add_filter('post_type_link', 'vv_cpt_acf_permalink', 1, 3);

    but… how to always add this parameter, even if someone wants to remove it from the browser bar?

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

The topic ‘Add URL parameter from ACF to CPT post’ is closed to new replies.