Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8723119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:33:15+00:00 2026-06-13T07:33:15+00:00

I have a custom loop with posts which are added from the custom meta

  • 0

I have a custom loop with posts which are added from the custom meta field checkbox. Only if the checkbox is checked, then the post is being added to the loop. I have a container that holds that loop. What i want to do is to check if that loop got any posts and if it is empty – just hide that container. Because otherwise when the loop is empty the container is remaining on the page:

<div>
  <ul>
  </ul>
</div>

This is the loop:

<?php 
/* Slider ------- */
$slider = new WP_Query('showposts=-1');
if ( $slider->have_posts() ):
?>  
<div>
    <ul>
    <?php while ( $slider->have_posts() ) : $slider->the_post(); ?>
        <?php if ( get_post_meta($post->ID, "mf_homeslider", true) == 'slider_on' ){ // Check if post was added to slider ?>
        <li>
        <?php if (has_post_thumbnail()) {  ?>
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail('large'); ?> 
            </a>
        <?php } ?>
            <div>
                <?php get_template_part('includes/post_meta'); ?>
                <h2>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </h2>
            </div>
        </li>
        <?php } ?>
    <?php endwhile; ?>
    </ul>
</div>
<?php 
endif;
wp_reset_query();
?>

Thank you in advance for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:33:16+00:00Added an answer on June 13, 2026 at 7:33 am

    You should filter your query by the custom field (see details here), it could also improve performance (an SQL condition vs. a while-if loop). After that, have_posts() returns whether the loop contains any posts.

    So instead of

    $slider = new WP_Query('showposts=-1');
    

    use

    $slider = new WP_Query(array(
        'meta_key' => 'mf_homeslider',
        'meta_value' => 'slider_on',
        'posts_per_page' => -1)
    );
    

    and there’s no further need for if ( get_post_meta(...

    (showposts is deprecated, use posts_per_page instead).

    Note: you are mixing braces and if-endif style, the latter would be much readable when nesting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem regarding a JSON string grabbed from WordPress custom post meta.
Basically I need a custom Wordpress post loop which outputs posts in the following
Basically I have a custom post type setup called Parts with over 5,000 posts
I have made a custom loop in WordPress and for some reason, the date
So I'm using a custom loop to output a custom post type on some
I have created a custom post type with an image gallery upload. Now I
I have setup a custom Post Type in WordPress and have a question. How
I have setup a custom post type in Wordpress. It is a section where
I have created a custom post type calles articles and a custom category like
Ok i have a custom post type on my blog called videos, where i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.