Support » Plugin: WP Custom Fields Search » Query_posts or get_posts

  • Resolved MrNikolsh

    (@mrnikolsh)


    Hello! Thanks for the plugin.

    1. If I display the search results through Query_posts, then instead of the specified parameters in the filter (by custom fields) I get the output of absolutely all records on the site.

    2. If I display the search results via get_posts, sorting by an arbitrary CENA field does not work.

    I do not use ACF. Changed the php version, did not help. What could be the problem. Thank!

    <div style="float:left;margin:20px 0 0 0%;width:100%;text-align:center;">Найдено товаров по Вашему запросу - <span style="color:#3ac128"><?php global $wp_query; echo $wp_query->found_posts;?></span></div>
    <div itemprop="articleBody" class="shina-textstr mycart_shelfItem">
    <?php the_posts_pagination( array(
    'screen_reader_text' => ' ',
    'show_all' => False, // показаны все страницы участвующие в пагинации
    'end_size' => 1, // количество страниц на концах
    'mid_size' => 1, // количество страниц вокруг текущей
    'prev_next' => True,  // выводить ли боковые ссылки "предыдущая/следующая страница".
    'prev_text' => __('«'),
    'next_text' => __('»'),
    ) ); ?>
    </div>
    <div itemprop="articleBody" class="shina-textstr mycart_shelfItem">
    <?php query_posts ( array ($query_string.'cat'=>get_query_var('cat'),'orderby'=>'meta_value_num','meta_key'=>'cena','posts_per_page'=>'24','order'=>'ASC'));
    if (have_posts()) {
    {echo '';}
    while (have_posts()) {
    the_post();?>
    <div class="kazhdblok mycart_shelfItem">
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <div class="miniatyri"><?php if ( in_category( array( 2,  ) )) { ?><div class="shmnt">Шиномонтаж в подарок!</div><?php } ?><img src="<?php if (has_post_thumbnail()) { $thumb_id = get_post_thumbnail_id();$thumb_url = wp_get_attachment_image_src($thumb_id,'full', true);echo $thumb_url[0];}else {echo 'http://megashina-taganrog.ru/upload/ftk/'.get_post_meta($post->ID, 'foto', true).'';}?>">
    </div></a>
    <div class="nazvtovar"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><h3><?php if ( in_category( array( 3,  ) )) { ?>
    <?php echo 'Колесный диск '; echo get_post_meta($post->ID, 'opis', true); ?>
    <?php } ?>
    <?php if ( in_category( array( 2,  ) )) { ?>
    <?php echo 'Автомобильная шина '; echo get_post_meta($post->ID, 'opis', true); echo' '; echo get_post_meta($post->ID, 'sezon', true); ?>
    <?php } ?></h3></a><div style="display:none" class="item_name"><a href=&quot;<?php echo get_permalink(); ?>&quot;><?php the_title(); ?></a> <a id=&quot;n-link&quot; href=&quot;&quot; download></a></div></div>
    <?php echo '<div class="tsennik-k item_price">';
    echo get_post_meta($post->ID, 'cena', true);
    echo '<span class="rub-k"> руб.</span><br></div><div class="k"><input type="number" class="item_quantity" min="1" value="1"/></div>';?>
    <div class="starcena">на <?php echo get_post_meta($post->ID, 'nomersklada', true); ?> складе <?php echo get_post_meta($post->ID, 'kolvonasklade', true); ?> шт</div>
    <?php echo '<div class="vkorz">
    <span class="dob item_add" tabindex="1" data-title=""><i title="в корзину" id="vko" class="sline-wallet"></i></span>
    </div></div>';?>
    <script>
    $('.item_price').each(function(){
    var str = $(this).text();
    $(this).html(str.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 '));
    });
    </script>
    <?php	}
    }
    ?>
    <noindex><div style="margin-top:50px;margin-bottom:210px;float:left;width:100%;"><?php the_posts_pagination( array(
    'screen_reader_text' => ' ',
    'show_all' => False, // показаны все страницы участвующие в пагинации
    'end_size' => 1, // количество страниц на концах
    'mid_size' => 1, // количество страниц вокруг текущей
    'prev_next' => True,  // выводить ли боковые ссылки "предыдущая/следующая страница".
    'prev_text' => __('«'),
    'next_text' => __('»'),
    ) ); ?></div>
    </noindex>
    </div>

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter MrNikolsh

    (@mrnikolsh)

    Query_posts worked on the old version of your plugin.

    • This reply was modified 1 year, 6 months ago by MrNikolsh.
    Plugin Author don benjamin

    (@dondon-benjamincouk)

    1. I changed the behaviour for query_posts in version 1.2.24 as it was restricting all queries used anywhere in the page. So e.g. a sidebar widget showing latest posts would only show posts related to the current search which wasn’t always desirable behaviour. There are other examples where this was fundamentally breaking people’s sites and meant some people couldn’t use the plugin.

    At the same time I added in a filter to allow the old behaviour to be restored. If you copy the following from the README.txt file into eg. your functions.php file:

    add_filter('wpcfs_should_override_current_query', function() { return true; })

    Essentially that filter is called for every call to query_posts and if it returns true then search parameters will be applied to the query. By default the search will only affect the main query (i.e. global $wp_the_query or $wp_query->is_main_query())

    • This reply was modified 1 year, 6 months ago by don benjamin.
    • This reply was modified 1 year, 6 months ago by don benjamin.
    • This reply was modified 1 year, 6 months ago by don benjamin.
    Plugin Author don benjamin

    (@dondon-benjamincouk)

    2. I’m not familiar with CENA fields is this related to the CENA theme? Unfortunately that’s a premium theme so I can’t test it without buying a copy.

    Thread Starter MrNikolsh

    (@mrnikolsh)

    Thanks for the answer! I myself created this theme. The CENA field is PRICE (Цена in Russian – cena).

    Added add_filter code ('wpcfs_should_override_current_query', function () {return true;}) to functions.php, sorting worked, but pagination stopped working. When switched off, the first page remains.

    <?php the_posts_pagination( array(
    'screen_reader_text' => ' ',
    'show_all' => False, // показаны все страницы участвующие в пагинации
    'end_size' => 1, // количество страниц на концах
    'mid_size' => 1, // количество страниц вокруг текущей
    'prev_next' => True,  // выводить ли боковые ссылки "предыдущая/следующая страница".
    'prev_text' => __('«'),
    'next_text' => __('»'),
    ) ); ?>

    What is the problem?

    • This reply was modified 1 year, 6 months ago by MrNikolsh.
    • This reply was modified 1 year, 6 months ago by MrNikolsh.
    Plugin Author don benjamin

    (@dondon-benjamincouk)

    Hi,

    I’m not able to reproduce this problem locally. I can set up a search locally with the same url structure as you but the pagination works correctly for me.

    The plugin doesn’t actually handle pagination itself so I think the problem may be somewhere else? Is there anywhere on your site to view a list that hasn’t been filtered by the plugin, e.g. a full listing of products or a category page or similar? If not, maybe you could set one up. Does the pagination work on those pages? If not then it’s unlikely to be a problem with this plugin.

    Thanks,
    Don

    • This reply was modified 1 year, 6 months ago by don benjamin.
    Plugin Author don benjamin

    (@dondon-benjamincouk)

    Ignore that, I’ve just found the category pages and they’re working fine so it is to do with the search results.

    Plugin Author don benjamin

    (@dondon-benjamincouk)

    I think this may be to do with the use of query_posts rather than to do with this plugin

    I’ve found this link which may help:
    https://codex.wordpress.org/Pagination#Removing_query_posts_from_the_Main_Loop

    Specifically this code for keeping the pagination when using query_posts:

    <?php 
    // query to set the posts per page to 3
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array('posts_per_page' => 3, 'paged' => $paged );
    query_posts($args); ?>
    

    Which I’d guess would still work like this:

    <?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array('paged' => $paged );
    query_posts($args); ?>
    

    If that doesn’t work it’s possible this will:

    <?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array('paged' => $paged, 'page'=>$paged);
    query_posts($args); ?>
    
    • This reply was modified 1 year, 6 months ago by don benjamin.
    Thread Starter MrNikolsh

    (@mrnikolsh)

    Don, this code helped me! + code for functions.php

    <?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array('order'=>'ASC','orderby'=>'meta_value_num','meta_key'=>'cena','posts_per_page'=>24, 'paged' => $paged, 'page'=>$paged );
    query_posts($args); if (have_posts()) {
    {echo '';} while (have_posts()) { the_post();?>

    Thanks for the help and great plugin! If you add Ajax to it, then he will not be equal.

    Thread Starter MrNikolsh

    (@mrnikolsh)

    Hello! How to make sure that empty OPTION are not displayed and are not SELECTED by default. The plugin’s default selection is “ВЫБРАТЬ ВСЕ”, but empty values are output from the database. Thank!

    Thread Starter MrNikolsh

    (@mrnikolsh)

    Decided.

    <script type="text/javascript">
    $('option').each(function(){
    if( $(this).text().trim() === '' )
    $(this).remove();
    });
    </script>
    
    <style>
    select option[selected=selected]:nth-child(2) {
    display:none;
    }
    </style>
    Plugin Author don benjamin

    (@dondon-benjamincouk)

    Sorry for the slow reply. Glad you got it working.

    Please feel free to rate the plugin here https://wordpress.org/support/plugin/wp-custom-fields-search/

    Thanks,
    Don

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Query_posts or get_posts’ is closed to new replies.