WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] how to filter the loop with custom fields (17 posts)

  1. henri
    Member
    Posted 4 years ago #

    Hi I use custom fields on my blog and I want to display in the loop only post with a specific custom field meta value.

    Currently I use this trick :

    <?php if (get_post_meta($post->ID, 'custom_field_disponibilite', true) == 'ND') continue; ?>

    But with this, the post number is inferior to the post number select in the admin area.

    I think I can use a add_filter or add_action inside the function.php of my theme but I have no idea how to implement it.

    Thanks for your help

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    Try adding:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
    	'meta_key' => 'custom_field_disponibilite',
    	'meta_value' => 'ND',
    	'paged' => $paged
    );
    query_posts($args);
    ?>

    just before the start of the Loop.

    http://codex.wordpress.org/Template_Tags/query_posts#Custom_Field_Parameters

  3. henri
    Member
    Posted 4 years ago #

    Thanks esmi for your help,

    This code works for the index.php but failed in category.php do you know how to insert this in the category template on my theme?

    Thanks

  4. esmi
    Forum Moderator
    Posted 4 years ago #

    Have you tried placing it before the Loop in category.php?

  5. henri
    Member
    Posted 4 years ago #

    Yep just before

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
  6. esmi
    Forum Moderator
    Posted 4 years ago #

    If it works in index.php, it should work in catgeory.php. What happens when you add it? Are there any other custom queries in category.php?

  7. henri
    Member
    Posted 4 years ago #

    I put my category.php code into this pastebin : http://pastebin.com/ma979473

  8. henri
    Member
    Posted 4 years ago #

    When I add the custom query it display all the post like in the index.php, it's strange I think it forget that it's a category page...

  9. esmi
    Forum Moderator
    Posted 4 years ago #

    From what I can see, it should work but then I have no idea how all of the various custom meta fields are being used on the site and how they might then affect the final display. What happens when you add the custom query to line 41?

  10. henri
    Member
    Posted 4 years ago #

    Yes I put the custom query on line 41 and I display posts like in index.php but with all categories instead of only the current category

  11. esmi
    Forum Moderator
    Posted 4 years ago #

    Try:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
    	'cat' => $this_cat_ID,
    	'meta_key' => 'custom_field_disponibilite',
    	'meta_value' => 'ND',
    	'paged' => $paged
    );
    query_posts($args);
    ?>
  12. henri
    Member
    Posted 4 years ago #

    Yep you are a king!
    Thanks a lot esmi

  13. henri
    Member
    Posted 4 years ago #

    To thank you for your help, I put a back link to your website here : http://webdesign.2803.com/ (live when after the next cache erase)

  14. esmi
    Forum Moderator
    Posted 4 years ago #

    Cool! Thank you. :-)

  15. henri
    Member
    Posted 4 years ago #

    You're welcome ;)
    Last question, to you know which argument use for taxonomy, search, and tag pages instead of
    'cat' => $this_cat_ID,
    (it's the same problem)

  16. henri
    Member
    Posted 4 years ago #

    Esmi I found why the code not properly work ;) I use the WP smart sort plugin and this plugin use query_var as you can see here : http://pastebin.com/m875d499. I think I can hack the plugin to restrict the result with my custom field query :

    'meta_key' => 'custom_field_disponibilite',
    'meta_value' => 'ND',

    But I don't know where to add my piece of code.

    Thanks for your help

  17. alpha_llama
    Member
    Posted 4 years ago #

    Thanks esmi.

    I was just having trouble getting pages in addition to posts to show. The link to the Custom Field Parameters you posted tipped me off:
    'post_type' => 'any'and it worked beautifully.

Topic Closed

This topic has been closed to new replies.

About this Topic