Ask Your Question
1

What is the method to include a fixed query value on specific pages and posts in WordPress?

asked 2022-01-10 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-05-13 01:00:00 +0000

ladyg gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-01-10 11:00:00 +0000

Seen: 14 times

Last updated: May 13 '22