Query variables to the URL

Support MB Custom Post Type Query variables to the URLResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #36272
    Régis EvennouRégis Evennou
    Participant

    Hello,
    In home page, latest posts from a CPT called 'actualite' are displayed. For each post, a custom field is displayed as a link. This link should filter posts with a meta_query by passing argument in url.

    Custom query string variables is registered :

    function add_custom_query_var( $vars ){
      $vars[] = 'residence';
      return $vars;
    }
    add_filter( 'query_vars', 'add_custom_query_var', 10, 1  );

    Url is modified to pass parameters to the template archive-actualite.php :

    
    esc_url( add_query_arg( 'residence', $value, site_url( '/actualite' ) ) )
    

    Wordpress do not find the page (404).

    Here is my CPT code in MB post Type :

    
    <?php
    add_action( 'init', 'your_prefix_register_post_type' );
    function your_prefix_register_post_type() {
    	$labels = [...];
    	$args = [
    		'label'               => esc_html__( 'Actualités', 'your-textdomain' ),
    		'labels'              => $labels,
    		'description'         => '',
    		'public'              => true,
    		'hierarchical'        => false,
    		'exclude_from_search' => false,
    		'publicly_queryable'  => true,
    		'show_ui'             => true,
    		'show_in_nav_menus'   => true,
    		'show_in_admin_bar'   => true,
    		'show_in_rest'        => true,
    		'query_var'           => true,
    		'can_export'          => true,
    		'delete_with_user'    => true,
    		'has_archive'         => true,
    		'rest_base'           => '',
    		'show_in_menu'        => true,'menu_position'       => 27,
    		'menu_icon'           => 'dashicons-megaphone',
    		'capability_type'     => 'page',
    		'supports'            => ['title', 'editor'],
    		'taxonomies'          => [],
    		'rewrite'             => [
    			'with_front' => true,
    		],
    	];
    
    	register_post_type( 'actualite', $args );
    }

    I read lot of forum posts, it should work...
    Url re-writting maybe needs to be configured. Permalink settings are set to /%postname%/
    I tried with different template (defaut and custom), wordpress never finds the page.

    I need help 🙂
    Thank you

    #36307
    Long NguyenLong Nguyen
    Moderator

    Hi,

    I'm not sure why this does not work on your site, you can try to use the absolute URL and real value like this

    esc_url( add_query_arg( 'residence', 'my-value', 'https://yoursite.com/actualite/' ) )

    to check if works. Or add variables directly in the URL and access the page

    https://yoursite.com/actualite/?residence=my-value

    #36311
    Régis EvennouRégis Evennou
    Participant

    Hi,
    Thank you for tips, i have already tried.
    I do not understand, it should be simple, i give up 🙁
    I will use a taxo instead.
    Have a nice day
    Régis

    #36321
    Régis EvennouRégis Evennou
    Participant

    Finally it works !
    I must be tired 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.