One way to include a fixed query value on specific pages and posts in WordPress is to use a custom function in the functions.php file of your theme. The function should use the pregetposts hook to modify the query args for the specific pages and posts that you want to add the fixed value to. Here is an example of the function:
function add_fixed_query_value( $query ) {
if ( is_single() && in_array( get_the_ID(), array( 1, 2, 3 ) ) ) {
$query->set( 'my_query_param', 'fixed_value' );
}
}
add_action( 'pre_get_posts', 'add_fixed_query_value' );
In this example, the function adds the query parameter my_query_param
with a fixed value of fixed_value
to the query for posts with IDs 1, 2, and 3. You can modify this function to suit your specific needs, such as targeting certain pages or using a different query parameter name and value.
Asked: 2022-01-10 11:00:00 +0000
Seen: 15 times
Last updated: May 13 '22