I cannot get tax_query in get_posts() to work with custom taxonomy

I have been trying to achieve this for the past two days but nothing works. I am attempting to build functionality to search any post, page, and a couple other custom post-types by a tag. I have created a plugin which registers a taxonomy called search_tag. search-tags.php require_once __DIR__ . '/includes/search-tags.class.php'; function search_tags_closure() { $search_tags = new Search_Tags; $search_tags->create_taxonomy(); } add_action('init', 'search_tags_closure'); search-tags.class.php class Search_Tags { const NAME = 'search_tag'; const LABEL = 'Search Tags'; protected function get_post_types() { $args …
Category: Web

How can I create 'future' and 'past' parameter for restAPI by filtering the CPT custom date field by greater than / less than current datetime?

add_filter( 'rest_events_query', 'filter_events_by_date_field', 999, 2 ); function filter_events_by_date_field( $args, $request ) { if ( ! isset( $request['filter_date'] ) ) { return $args; } $filter_date = sanitize_text_field( $request['filter_date'] ); $datetime = current_time('mysql'); if( $filter_date == 'future' ) { $filter_date_meta_query = array( 'key' => 'date', 'value' => $datetime, 'type' => 'DATETIME', 'compare' => '>=' ); } else { $filter_date_meta_query = array( 'key' => 'date', 'value' => $datetime, 'type' => 'DATETIME', 'compare' => '<' ); } if ( isset( $args['meta_query'] ) ) { …
Category: Web

Fix wp_term_relationships slow query in get_posts

I used the Query Monitor plugin to find slow query on my WordPress site. The below-mentioned WP_Query->get_posts() takes about 0.3446 query time out of the total Database Query time of 0.3976. SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND wp_posts.ID NOT IN (203598) AND ( wp_term_relationships.term_taxonomy_id IN (17) OR wp_term_relationships.term_taxonomy_id IN (11652,20693,21952,23971,24907,24908,25928) ) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 6 I'm guessing it …
Category: Web

pre_get_posts with WP_Query to prevent posts from specific tags

I found this post about how to change a specific query but can't find a way to do the following: I have several queries on homepage retrieving posts from categories, posts from "New"s, posts from "Recipes", posts from "Promo" etc I have a slider area at the top that display posts from all categories that uses the tag "featured" How can I prevent all other queries on home from retrieving posts using "featured" tag so posts using the tag do …
Category: Web

the_post() within switch_to_blog() altering my excerpt

I have a strange problem I'm facing when using WP_Query with multisite in a plugin I'm writing which hooks into add_meta_boxes. I'm wanting to display all post titles in a dropdown in a meta box. While adding or editing a post in blog no. 2 (it could be any blog no.), I am looping through blog no. 1 and getting post titles from a custom post type to populate in a select dropdown. This works as it should, but in …
Category: Web

Fill the exclude array() in get_posts()

I'm using this code to populate the dropdown menu in a meta box. $parents = get_posts( array( 'post_type' => 'jhk_story', 'orderby' => 'ID', 'order' => 'ASC', 'numberposts' => -1, 'exclude' => array(121, 131, 138), ) ); This post_type contains the parents of a post_type 'jhk_frames', which are the only child. So, the children contain in their post_parent field the post_ID of their parent. The parent contains in their post_parent field still the default zero. My question is: how do I …
Category: Web

Display if author page is author page of current user

in my multi author website I have a button that users can upload their avatars. I need to display this button in author page. I can do it. I added this button to author page. The problem is that author can see this button in other author's page too . That's why I need to display this button in author page of current user. I am so sorry I have no any reference code. I have no any idea how …
Category: Web

Working Bootstrap Carousel Conversion to WP - Technical Questions

I have coded a working Bootstrap carousel, it needs to be converted to WP. It's a small chunk of the site, but as a Wordpress newbie, am stuck in understanding the technical issue. So here's what I am trying to do: To show all those posts that have a featured image uploaded to show as a part of the Bootstrap carousel, then to limit the posts per page as needed. As a first step, I thought to not use WP_Query …
Category: Web

Meta query with JSON value

Im using post meta to store some value. Im having the following in wp_postmeta table: meta_key mykey meta_value a:2:{i:0;s:1:"5";i:1;s:1:"2";} I need to return posts which have meta_value of 2. My query: $args = array( 'post_type' => 'post', 'meta_key' => 'mykey', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'mykey', 'value' => '2', 'compare' => 'IN', ) ) ); $the_query = new WP_Query($args); But no posts returned... Where i made mistake?
Category: Web

How to pass taxonomy terms to WP_Query along with $args?

I created a cpt and custom taxonomy associated with it. Lets say custom taxonomy name is Sports. I added terms to it - football, basketball. Now, i want to fetch post titles tagged under football and basketball separately on same page.(Note:- i am using a custom page template to show list of articles from CPT). $args = array( 'post_type' => 'sports', 'tax_query' => array( array( 'taxonomy' => 'sports_category', 'field' => 'slug', 'terms' => 'football' ), ), ); $myquery = new …
Category: Web

Query multiple post types, but different order for each

I have a query which searches 2 post types as below: $args = array( 'post_type' => ['post', 'promotion'], 'posts_per_page' => 12, 'paged' => $paged, 's' => isset($_GET['search']) ? $_GET['search'] : '', ); This orders them by default, by published date. I need to add a third post type events. This however needs to be sorted by a custom field event_date asc and to disregard those which the event_date is in the past. Does anyone have any suggestions on how this …
Category: Web

Array to string conversion on array_map

I'm using array_map to sort out an array with all of my _octopud_id's. var_dump($offices): Returns the following: array (size=500) 0 => array (size=1) 'id' => string '1382' (length=4) 1 => array (size=1) 'id' => string '1330' (length=4) I need to input that array integer into the 'employees/' section but I can't figure out how - If I hardcode employees/6 I get the following result: object(stdClass)[14592] public 'id' => int 6 What could I be doing wrong? I keep getting the …
Category: Web

Custom search: Search posts but display parent page in results

I'm using Wordpress 3.5.1 with a modified twentytwelve theme. My main site structure is based on the Smooth Slider plugin (jQuery Cycle). I have Pages that contain only a shortcode (for example, [smoothslider id='4']) to display Posts assigned to a slider. When I do a search on the site it returns result for individual posts only (and not for pages, of course). This is not what I want as there is no slider anymore. Those posts are not supposed to …
Category: Web

search.php to search only the post title

I'm trying to rewrite my search.php to search only for the post titles because I have a ton of posts published to my site (around 400k), which slows down the search speed to about 20s per search. <?php get_header(); $search_term = $_GET['s']; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $posts = new WP_Query( array( 's' => "$search_term", 'ep_integrate' => true, 'post_type' => 'post', 'posts_per_page' => 21, 'orderby' => 'date', 'order' => 'DESC', 'paged' => $paged, ) ); ?> <?php if …
Category: Web

how to get dynamic data on wordpress from codeigniter?

I made a website with WordPress and Codeigniter. I create 4 pages on Codeigniter then one more blog page in WordPress. I get the footer data dynamically on the Codeigniter based site. I need to take the same footer data dynamically on my WordPress pages.
Category: Web

Get and Trim Full Post Content in WP Query

Simple task, but it isn't working: <?php $content = apply_filters( 'the_content', get_the_content() ); $contentWithoutHTML = wp_strip_all_tags($content); $pos = strpos($contentWithoutHTML, " ", 100); $contentFinal = substr($contentWithoutHTML,0,$pos ); echo $contentFinal . "..."; ?> My post is way over 100 characters long (with spaces), and yet I get strpos(): Offset not contained in string error, which leads me to believe it isn't actually pulling the entire content string. But I applied filters like I believe I should... please assist. Also sometimes even if …
Category: Web

Get post from Category by Priority

I want get 3 posts from event custom post type. But i want to do that by category priority. If "featured" category has 3 upcoming event then i want to show 3 posts from it, if "featured" category doesn't have 3 upcoming events then i want to get posts from other category+posts without any category too. The end result will return 3 upcoming event. I'm a little confuse about how can i add that condition parameter on WP_Query. Current code(that …
Category: Web

Display Wordpress Post By Date

I want to show Wordpress post by day/date in homepage. ex: Monday, January 03 post 1 post 2 post 3 Sunday, January 02 post 1 post 2 post 3 Saturday, January 01 post 1 post 2 post 3 What Wordpress query should I use? Thanks. Sorry for bad english.
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.