0

I have custom field "Fruit" with value "Orange" and others, I want to use a short code that should display all posts on a page for "Orange" value by URL parameter. for example below URL should show all posts on a page related to "orange": www.domain.com/?fruit=orange

I've very basic expertise of WordPress development and couldn't find any solution yet.

Currently I'm using this short code to display posts, but want to filter posts by URL parameters

function my_pre_get_posts( $query ) {

$the_query = new WP_Query('meta_key=fruit');

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); 
echo '<div class="fruits">';
    echo '<div class="content">'; the_post_thumbnail('thumbnail');  echo '</div>';
    echo '<div class="title">'; the_title(); echo '</div>';
    $slug = get_post_field( 'post_name', get_the_ID() );

echo '<div class="readMore"> <a href="'.$slug.'"> Read More </a>';

echo '</div>';

endwhile;

// Reset Post Data
wp_reset_postdata(); 

}

add_shortcode('pre_get_posts', 'my_pre_get_posts');

3
  • Welcome! What have you tried to accomplish this? Where is your shortcode? Commented Nov 21, 2023 at 19:47
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Nov 21, 2023 at 21:33
  • just added my short code , please have a look Commented Nov 22, 2023 at 8:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.