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

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

returning the currect URL for nested posts

I have a posttype called (article) witch have a meta_key called (parents_ids). So the article can be related to multiple articles. I want to return the currect URL depending on the parent post For example: 1-If the user click on the child-article comming from parent-article-1 i want the url to be example.com/parent-article-1/child-article. 2-If the user click on the child-article comming from parent-article-2 i want to return the url example.com/parent-article-2/child-article
Category: Web

Rest Api How to get results of child categories with one api call

Example: If I have a parent category 1 with child categories 2,3,4 etc instead of a call like this: /wp/v2/posts?categories=1,2,3,4 i want a call like this (imaging code) /wp/v2/posts?categories=1&includeallchilds=true that will include all articles from the child and sub-child of 1. how is this possible? I don't want my application make 2 api calls to find all childs of the categories of 1.. this query can done what i need, but i cant include this on rest api call, $args …
Category: Web

wp_remote_post and form post

Actually it is more general question but now i am trying to use it on wordpress site so I preferred to post it here. I am trying to understand the difference between remote post and form post. When I query a string and post it to remote server, I get the result as string and with "print $response" see it on browser console. But I want it to be displayed as html on browser. Am I expecting wrong thing or …
Category: Web

How to optimize 'select found_rows()' query? Several 'high load average' alerts daily

I have 5,000 regular posts, 6,000 posts in one post type and 2,000 posts in another post type. Needless to say, that has made the wp_posts table quite large. Not to mention, I have custom taxonomies set up to mimic the post types so for every Company A entered into Company post type, there is Company A, I manually entered Company A into Company taxonomy. That way, when I do a regular post about Company A, I can effectively tag …
Category: Web

custom query to get posts

I want to create custom links like mydomain.com/custom_page/cat=ABC&tag=XYZ So that when a user clicks on the link s/he can see all posts in the category 'ABC' having tag 'XYZ' For this I've created a custom template with the following code <?php /* Template Name: MyCustomTemplate */ ?> <?php get_header(); ?> global $wp_query; get_query_var( 'cat' ); get_query_var( 'tag' ); <?php get_footer(); ?> I don't know how to query for the posts in the category 'ABC' with the tag 'XYZ' I checked …
Category: Web

query_posts and only show results if a custom field is not empty

How do I query_posts and only show results if a custom field is not empty or has a value. I want to put in a URL in a custom field and only show these pages if there is a URL? current code but I can't figure out the rest: $args = array( 'posts_per_page' => '10', 'post_type' => 'programmes', 'orderby' => 'meta_value_num', 'meta_key' => 'popularityfig', 'order' => 'DESC', );
Category: Web

Change the destination of the URLs in post.php

The post.php screen lists posts vertically in the admin area [and other post types]. Each item's post_title is a hyperlink, directly above some inline choices. Both the title link, and the "Edit" inline selection, redundantly go to the same destination, namely: http[s]://{yoursite.tld}/wp-admin/post.php?post={postID}&action=edit I want to filter the post_title URL href using PHP. Specifically, I want to set a custom destination to the URL, based on metadata stored in the post. i.e post_ID:123 has post meta data: {'custom_URL' : 'http:somesite.com'} In …
Category: Web

Order post by year DESC and month ASC

I have a worpresss 4.0.12 website; i created a custom template category-$id.php in which I have to show post ordering them by Year DESC and by month ASC (or a custom field ASC), the result should be something like this: 2017 Post 10-01-2017 Post 25-01-2017 Post 05-03-2017 Post 06-12-2017 2016 Post 10-01-2016 Post 05-03-2016 Post 06-12-2016 2015 [...] How can I do? I Tried width query_posts($args) but I can't order by a second field
Category: Web

Hot to check if new posts have been published since page load and notify online users?

I want to show a div notifying online users that there are new posts since they have accessed the page, in the index and also if new posts have been added in the category or tag they are browsing. I have been trying for hours to find a script that does exactly what I need, but I have not found anything exact, and my programming level is very low. Can anybody help me please?
Category: Web

WP 5.8 "Query Loop" block: where to place custom query?

In the recent wordpress.org article outlining the new Query Loop block, it says the "Inherit query from template" option allows you to "customize the query that the loop relies upon ... WordPress will otherwise rely on the template being used to determine what posts appear". There appears to be no further guidance on this. What does this mean, and where does it find a query for these posts? If I have a custom taxonomy defined, how would I pull from …
Category: Web

Pull in posts using post meta for REST API

So I wasn't able to find much detail about this, so I wanted to ask the community to see if I can get an answer. So let's say that I have the following: $this->global_endpoint = 'https://example.com/wp-json/global/v1/posts` which is the REST API endpoint for pulling in posts for WordPress. Then I am calling wp_remote_request such as this: $response = wp_remote_request(add_query_arg( [ 'page' => 1, 'per_page' => 3, 'post_status' => 'publish', ], $this->global_endpoint)); Does anyone know if it's possible to get a …
Category: Web

Order posts by custom field and if custom field is empty return remaining posts

I have an archive page ordered by a numeric value in a custom field. This returns the ordered posts correctly, but does not display posts that don't have a custom field. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("paged=$paged&cat=7&posts_per_page=24&meta_key=custom_order&orderby=meta_value_num&order=ASC"); What is the best way to return the ordered posts correctly, followed by any posts that do not have a custom field value associated with them? Edit: Just to elaborate -- The final result that I want to accomplish is a …
Category: Web

Bypass "supress_filters" in WP Query

Apparently, for some illogical reason, the developers decided that the only way to fetch posts in all languages, is to add supress_filters=true to the WP_Query (instead of having like say, a language_code=all option). Anyways, I am in a situation where I need to fetch posts in all languages, but ALSO modify the WP_Query using filters. Is there a way to enforce that my filters are being added to the query, even though supress_filters is set to false? This is the …
Category: Web

Query Custom Meta Value with Increment

I have a Query searching for a meta value I have added to my posts (meta-select). I have it feeding out in a loop with 4 results. Let's say I want this loop to start with the meta value "2" and work its way up "3","4","5" each time adding 1 to the value in the loop. Here is my current code which obviously just queries for the value "2" where do I add an increment in this function? <?php // …
Category: Web

Include current post into loop

I have a related posts loop ( in every single post page ) based on post title. source : https://wpza.net/get-related-posts-based-on-the-posts-content-in-wordpress/ But I also need to include current post into this loop 'post__in' => $similar, . Please can anyone help me how can I do this function custom_related_posts() { $current_id = get_the_ID(); $current_content = get_the_title( $current_id ); $similar = array(); $scores = array(); $percentage = 0; $output = ''; function compare_content( $current_content, $comparison_content ) { $current_content = trim( strtolower( $current_content ) …
Category: Web

Get latest posts from multisite

I've created a multisite on a local VM and installed my theme. Now I'm trying to get the latest posts from all the sites in order to display them ordered by last updated on homepage. From what I've read so far, the best option should be creating a custom template for my home page. Is it correct? So I take the code in my template page.php but I cannot understand how to change it. <?php /** * The template for …
Category: Web

Getting movie and serial on actor page

I'm building a cinema site, I came with a problem, let me explain: Movies- are stored in Post's Actors- are stored in Post Type - persoane ( taxonomy= lista) Serials - are stored in Post Type - serial ( taxonomy= seriale) I use Advance Custom Field to link the Person to the movie - AFC (http://www.advancedcustomfields.com/) How I add: I add new actor post to post type ( persoane), after i add the movie (in post's), in sidebar(post's, movie page) …
Category: Web

About

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