Using get_posts WordPress - php

I am attempting to use get_posts() to retrieve posts that have certain topics attached to them, in my custom post type named 'leaders'
Here is my query.
$args = array(
'post-type' => 'leaders',
'meta_query' => array(
array(
'key' => 'topics',
'value' => '1773',
'compare' => 'LIKE'
)
)
);
$test = get_posts( $args );
If I var_dump the posts meta data i get this
topics a:4:{i:0;s:4:"1773";i:1;s:4:"1783";i:2;s:4:"1763";i:3;s:4:"1753";}
However my get_posts just returns null, can anyone see why?

Related

How i can compare value in wordpress custom post type loop using taxonomy as key

I am trying to search posts by comparing the value that i am getting from URL with taxonomy of the post. But i couldn't get it right. Here is my code.
$vendita = $_GET['vendita'];
$the_query = new WP_Query(
array(
'post_type' => 'custom_post_type',
'taxonomy' => 'taxonomy_name',
// 'paged' => $paged,
'meta_query' => array(
'key' => 'taxonomy',
'value' => $vendita,
'compare' => 'LIKE'
)
)
);
Thanks in advance.
First, if you are going to take parameters, you must always sanitize them, like with $vendita = sanitize_text_field($_GET['vendita']);.
This is not tested, but I think what you are looking for is this WP_Query
$the_query = new WP_Query(
array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy_slug',
'field' => 'slug',
'terms' => $vendita,
),
),
)
);
Note the 'field' can be the term_id or the name instead of the slug, I do not know what $vendita is exactly.
Note tax_query is a nested array, it must be that way.
Note: You can replace array() with the shorter [].
Note: From docs: If you use the_post() with your query, you need to run wp_reset_postdata() afterwards to have template tags use the main query’s current post again.

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

WP Query argument to search by post title

I am trying to filter my posts by post title. I can use the code below perfectly to filter by anything in postmeta, but i want to get results that match against the post title. How do i change this so it searches wp_posts as opposed to wp_postmeta? I want to search purely by title of the post.
'meta_query' => array(
array(
'key' => 'post_title',
'value' => $keywords,
'compare' => 'LIKE'
)
)
Not best solution but it works!
$args = array(
'posts_per_page' => 5,
's' => $keywords,
);

reuse wp query with different meta query

I am querying a custom post type. I would like to output posts which fall after a custom date field which I can do fine. But then I want to reuse the same basic query to output posts which fall before the custom date field.
So I have
$today = date('Ymd');
$args = array(
'post_type' => array( 'mypoststype' ),
'posts_per_page' => '3',
'meta_query' => array(
array(
'key' => 'mypoststype_date',
'compare' => '<=',
'value' => $today,
)
),
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
$myposts= new WP_Query( $args );
then I output the necessary contents in a loop. All fine.
Next I want to have posts from the same type ordered by the same key but with compare set to '>='.
Is there some way to do this without calling a whole new query?

Pass arguments into search results

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

Categories