Pass arguments into search results - php

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!

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' => '='
)
)
);

Related

Exclude Featured Posts in Wordpress 'Recent Posts' Function

Can I use the "exclude" array in the wp_get_recent_posts function to exclude Featured Posts? I have a plugin called NS Featured Posts which pulls featured posts through a key in the wp query i.e.:
$custom_query = new WP_Query(
array(
'post_type' => 'post',
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes'
)
);
could I somehow use this to target and exclude NS Featured Posts in the wp_get_recent_posts call e.g.:
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 3,
'exclude' => (the ns featured posts)
));
Thanks for any insight.
So I tested it now and getting posts without a certain meta key in one query is not possible.
But: You can exclude them from another query like so:
$featured_posts = get_posts( [
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'fields' => 'ids',
] );
query_posts( array( 'post__not_in' => $featured_posts ) );
while ( have_posts() ) : the_post();
$output .= '<li>'.get_the_title().'</li>';
endwhile;
wp_reset_query();
Functions such as wp_get_recent_posts() can accept all of the same arguments as WP_Query. While the documentation only lists a handful of parameters, the full set are available to you.
You had suggested using exclude in your query however that's going to want the IDs of the posts to exclude. You could of course grab those first but that's not going to be the most efficient solution.
The way to do this in a single query is with meta query options. The posts are being tagged with a meta key and a meta query will allow you to exclude those. You'll need to check both for the existence of the meta key and the value being 'yes'.
Example:
$recent_posts = wp_get_recent_posts( array(
'numberposts' => 3,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => '!=',
),
array(
'key' => '_is_ns_featured_post',
'compare' => 'NOT EXISTS',
),
)
) );
Reference: https://codex.wordpress.org/Class_Reference/WP_Meta_Query

How to get category by ID from a custom post type in a Wordpress query

I am trying to build a WP_Query and I want to fetch only the posts where the taxonomy (custom post type category with the name "give_forms_category") is "18". I have a working query for the regular post type but I'm trying to adapt it for my custom post type:
$the_query = new WP_Query( array (
'posts_per_page' => $atts['posts'],
'post_type' => array( 'give_forms' ), array( 'cat' => 18 ) )
Can someone give me a hint?
with custom fields you should use a special approach, explained in WP Codex
You might have something like this:
$the_query = new WP_Query( array (
'posts_per_page' => $atts['posts'],
'meta_key' => 'give_forms_category',
'post_type' => 'post',
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'give_forms_category',
'value' => 18,
'compare' => '='
),
)
)
);

Wordpress query based on ACF Datepicker values results in no posts

So I added some custom fields to the post posttype, namely event_startdate and event_enddate. My goal is to show all events that are currently ongoing (so current date is between start and enddate). However, no posts are showing up with my current code.
This is what I've got:
<?php
$today = date('Ymd');
$args = array (
'posts_per_page' => '9',
'order' => 'ASC',
'orderby' => 'id',
'meta_query' => array(
array(
'key' => 'event_startdate',
'compare' => '>=',
'value' => $today
),
array(
'key' => 'event_enddate',
'compare' => '<=',
'value' => $today
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
and then the rest of the loop. I know the loop works, because all posts show up when the meta_query is removed. What could the issue be? I've experimented with some different dateformats (hardcoded), but that didn't seem to fix it. Tried other solutions posted online as well, but none seemed to fix the issue.
EDIT: Started working on another query in the meantime, this time using event_featured, a TRUE/FALSE field. Code is the same as the previously mentioned code, except for the args. Doesn't return posts either:
$args = array (
'post_type' => 'post',
'posts_per_page' => '9',
'order' => 'ASC',
'orderby' => 'id',
'meta_query' => array(
array(
'key' => 'event_featured',
'value' => '1',
'compare' => '=='
)
)
);
I did not see in your code where you declared the post type to query. Add the post type to the query, see the first line after the first array. Try use this:
$args = array (
'post_type' =>'posttype',
'posts_per_page' => '9',
'order' => 'ASC',
'orderby' => 'id',
'meta_query' => array(
array(
'key' => 'event_startdate',
'compare' => '>=',
'value' => $today
),
array(
'key' => 'event_enddate',
'compare' => '<=',
'value' => $today
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
Found the issue. I created the fields in PHP. Using the admin interface to create them worked for me. So if you're also struggling with this, delete your fields in php and create them again in Wordpress.
If you want to include them with your theme, export the fields to PHP code with the export tool in ACF.

WP query filter posts based on url arguments

I have a set of wordpress postst that have various metafields. I'm trying to filter through these posts depending on the arguments given in a url. For example.
www.example.com/?artist=franksinatra&mood=
Should bring up all posts with frank sinatra as the artist field, but ignoring the mood field because its blank. That works fine. But I'd like to narrow my search even further. So when I add additional arguments such as:
www.example.com/?artist=franksinatra&mood=sad
All posts with that field combination should show up. Here's the beginning of my loop that allows me to search using one argument but not additional ones. Thank you.
<?php
$queryArtist = $_GET['artist'];
$queryMood = $_GET['mood'];
$paged = get_query_var('paged');
$loop = new WP_Query( array(
'posts_per_page' => 5,
'post_type' => 'catalog',
'paged' => $paged,
'order_by' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'artist',
'value' => $queryArtist,
'compare' => '=',
)
)
)
);
while ( $loop->have_posts() ) : $loop->the_post(); ?>
you can add additional queries like this:
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'artist',
'value' => $queryArtist,
'compare' => '=',
),
array(
'key' => 'mood',
'value' => $queryMood,
'compare' => '=',
)
)
by switching the relation from "OR" to "AND" both conditions have to be true

Ordering Wordpress posts by multiple meta values

Using get_posts(), I need to first retrieve posts that fall on a certain day (the day is set by a custom field - just the date, not time). I do this by using a meta key/value. Then, I need to order these posts based on the time of day (which is a separate custom field, just time, not date). So essentially I need to pull in all the events that fall on a given day, and order them according to the time.
First I grab the day, using a custom field:
if ( get_field('festival_day') ) {
$day_stamp = get_field('festival_day');
}
Then I set my arguments for the query:
$args = array(
'posts_per_page' => -1,
'post_type' => 'event',
'meta_key' => 'event_date',
'meta_value' => $day_stamp
);
$events = get_posts( $args );
So.. the question is, how do I query the other custom field (which is the start time), and then sort by that time? The time field key is event_start_time.
Thanks!
You can user WP_Query to retrieve your events and you can query it something like below:
$args = array(
'post_type' => 'event',
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'event_start_time',
'meta_query' => array(
array(
'key' => 'event_start_time',
'value' => 'yourValue_here',
'compare' => '>='
),
array(
'key' => 'event_date',
'value' => $day_stamp,
'compare' => '='
)
)
);
$query = new WP_Query( $args );
UNTESTED but it should work.
Check this code, it should work to sort your post according to time from resulting post of day.
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'event_start_time',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'event_date',
'value' => $day_stamp,
'compare' => 'IN'
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>'. get_the_title().'</li>';
}
echo '</ul>';
} else {
// no posts found
}

Categories