WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] how to filter the loop with custom fields

[Resolved] how to filter the loop with custom fields

  • 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

Viewing 15 replies - 1 through 15 (of 16 total)
  • esmi

    @esmi

    Forum Moderator

    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

    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

    esmi

    @esmi

    Forum Moderator

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

    Yep just before

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    esmi

    @esmi

    Forum Moderator

    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?

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

    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…

    esmi

    @esmi

    Forum Moderator

    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?

    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

    esmi

    @esmi

    Forum Moderator

    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);
    ?>

    Yep you are a king!
    Thanks a lot esmi

    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)

    esmi

    @esmi

    Forum Moderator

    Cool! Thank you. 🙂

    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)

    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

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘[Resolved] how to filter the loop with custom fields’ is closed to new replies.