• Resolved henri

    (@riri23)


    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)
  • 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.

    https://codex.www.ads-software.com/Template_Tags/query_posts#Custom_Field_Parameters

    Thread Starter henri

    (@riri23)

    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

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

    Thread Starter henri

    (@riri23)

    Yep just before

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

    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?

    Thread Starter henri

    (@riri23)

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

    Thread Starter henri

    (@riri23)

    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…

    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?

    Thread Starter henri

    (@riri23)

    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

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

    Thread Starter henri

    (@riri23)

    Yep you are a king!
    Thanks a lot esmi

    Thread Starter henri

    (@riri23)

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

    Cool! Thank you. ??

    Thread Starter henri

    (@riri23)

    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)

    Thread Starter henri

    (@riri23)

    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 : https://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 ‘how to filter the loop with custom fields’ is closed to new replies.