Query variables to the URL
Support › MB Custom Post Type › Query variables to the URLResolved
- This topic has 3 replies, 2 voices, and was last updated 2 years, 11 months ago by
Régis Evennou.
-
AuthorPosts
-
June 1, 2022 at 8:42 PM #36272
Régis Evennou
ParticipantHello,
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 youJune 3, 2022 at 12:34 PM #36307Long Nguyen
ModeratorHi,
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
June 3, 2022 at 2:26 PM #36311Régis Evennou
ParticipantHi,
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égisJune 3, 2022 at 7:54 PM #36321Régis Evennou
ParticipantFinally it works !
I must be tired 🙂 -
AuthorPosts
- You must be logged in to reply to this topic.