dtxpz8785 2016-07-08 15:27
浏览 32

将参数传递给搜索结果

I am creating a search function for a custom post type on my wordpress site, and I need to filter out search results based off an ACF True/False field. I have to use WP_Query to pass arguments since the generic wordpress loop does not allow that, but when I use WP_Query the query returns all the posts based on the arguments I passed, and disregards the actual search term.

<?php $args = array( 
  'post_type' => 'work',
  'posts_per_page' => -1,
  'meta_query' => array(
    array(
      'key' => 'work_hidden',
      'value' => '0',
      'compare' => '=='
    )
  )
); 

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) : 
  while ( $the_query->have_posts() ) {
    $the_query->the_post();
    include "partials/work-card.php";
  } 
endif; ?>

How can I use WP_Query to include the search term and the arguments.

Thanks so much!

  • 写回答

1条回答 默认 最新

  • dongxin2734 2016-07-08 15:42
    关注

    First the 'compare' when you want equals in SQL MUST be a single '=';

    Next you have to put the search parameter as from the Documentation, assuming is $_GET['search']:

    $args = array( 
        'post_type' => 'work',
        'posts_per_page' => -1,
        's' => $_GET['search'],
        'meta_query' => array(
             array(
                'key' => 'work_hidden',
                'value' => '0',
                'compare' => '='
            )
        )
    );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?