How to skip certain links on adjacent posts in Wordpress? - php

I use the following code to fetch the links of the previous and next posts respectively, from the single post template;
<?php echo get_permalink(get_adjacent_post(false,'',false)); ?>
<?php echo get_permalink(get_adjacent_post(false,'',true)); ?>
My question is, please, if there are certain posts I'd like these codes to skip, and simply go to the ones right after, could I do that somehow using custom fields, or otherwise how can I make Wordpress skip a certain link when it comes up and fetch the next adjacent one without first going to the one I'd like to skip and then redirect or something, but rather echo the correct one right away..?
Thank you very much!
Alex

You can approach this in different ways. The easiest solution would probably be to use an "excluded category" (second parameter), e.g. exclude posts from category with term ID 5:
get_adjacent_post( false, '5', false )
Another option is to use the get_previous_post_where and get_next_post_where filters to modify the SQL query.
You could store in the options table an array of post IDs to be excluded, here's an example how you could exclude all sticky posts:
add_filter( 'get_previous_post_where', 'so16495117_mod_adjacent' );
add_filter( 'get_next_post_where', 'so16495117_mod_adjacent' );
function so16495117_mod_adjacent( $where ) {
return $where . ' AND p.ID NOT IN (' . implode( ',', get_option( 'sticky_posts' ) ) . ' )';
}
Or, as you suggested in your Q, you could filter out posts that have a certain post meta key, e.g. my_field:
add_filter( 'get_previous_post_where', 'so16495117_mod_adjacent_bis' );
add_filter( 'get_next_post_where', 'so16495117_mod_adjacent_bis' );
function so16495117_mod_adjacent_bis( $where ) {
global $wpdb;
return $where . " AND p.ID NOT IN ( SELECT post_id FROM $wpdb->postmeta WHERE ($wpdb->postmeta.post_id = p.ID ) AND $wpdb->postmeta.meta_key = 'my_field' )";
}

Related

How can I list all editors of a WordPress page, including those who edited (saved page) without creating a proper revision?

I'm trying to display a list of the people who have authored AND edited a page in Wordpress, not just the people who have authored revisions. the_modified_author() gives me the name of the person who last saved the page. birgire suggested this code to get the list of all editors, but it seems to only show the authors of the original post and all revisions, and doesn't include the names of those who saved a change that wasn't actually a revision:
function get_the_modified_authors_wpse_99226(){
global $wpdb;
$authors = array();
$results = $wpdb->get_results( $wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE (post_type = '%s' AND ID = %d) OR (post_type = 'revision' AND post_parent = %d) GROUP BY post_author", get_post_type( get_the_ID() ), get_the_ID(), get_the_ID() ) );
foreach($results as $row){
$authors[] = get_the_author_meta('display_name', $row->post_author );
}
return implode(", ", $authors);
}
Sometimes people edit a page (for example, they make an edit to a custom field) and then update the post. When that happens, they would be listed under the_modified_author() but would not be considered an author of a revision.
What I need is a combined list of both revision authors AND those people who have edited (saved) the page without creating a revision. Sort of like the function for modified_author but without the '_edit_last', true part.
Is this possible?
Please try the below code :
$revisions = $wpdb->get_results("select * from {$wpdb->posts} where post_parent={$post_id} and post_type='revision'")
You may need to do some change according to your need.

Wordpress Search with Multiple Post Meta Value Keywords (No Plugin)

I've searched everywhere for an answer to this and keep finding different suggestions, but none that seem to fit my particular needs. I currently have a website using a custom built search (no plugin). Website: GayBarsIn.com
The search functions I found work for searching custom post titles, post meta data (Advanced Custom Fields) and a combination of both title and post meta data, but only one post meta keyword works. I'm trying to find a way to search multiple post meta keywords.
For instance: The site shows bar listings.
If I search for a single keyword... say "Cleveland," it will return all listings with that keyword (whether in the title or post meta).
If I search for "Cleveland Ohio," it will return only those values found first in the title, then in the post meta, but just one instance of post meta. So if the title has Cleveland in it and the post meta has Ohio, it will return those results, but it will not show those that contain two instances of post meta data with keywords "Cleveland Ohio."
What I need for it to do is show results using multiple search terms for ALL post meta (custom fields). Example: "Cleveland and Ohio" or "Nightclub and Lounge and Houston"
Here is the code I found that works okay for a single keyword search:
/* ========================================
* SEARCH CUSTOM POST TYPES
* ======================================== */
function searchfilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type', array('listing'));
}
return $query;
}
add_filter('pre_get_posts','searchfilter');
function cf_search_join( $join ) {
global $wpdb;
if ( is_search() ) {
$join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
}
return $join;
}
add_filter('posts_join', 'cf_search_join' );
/**
* Modify the search query with posts_where
*
* http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
*/
function cf_search_where( $where ) {
global $pagenow, $wpdb;
if ( is_search() ) {
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
}
return $where;
}
add_filter( 'posts_where', 'cf_search_where' );
/**
* Prevent duplicates
*
* http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
*/
function cf_search_distinct( $where ) {
global $wpdb;
if ( is_search() ) {
return "DISTINCT";
}
return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );
About half-way down the code, this section seems to be what should be edited, but I can't figure out how. I've tried switching 'OR' to 'AND' but that returns no results.
if ( is_search() ) {
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
}
I apologize if this has been asked before. I've looked for a few days and found others circling around what I'm trying to do, but not specifically enough for it to work for me. Any help or guidance would be much appreciated!
You should create your own form with input field for search, and catch data after post. You could split the words for example with explode() and put them into an array. You need to check if words exists as tag, so you need to loop through your array and check if it exists in get_tags($args). After that you should have an array with only useful tags. Now you can use get_posts()
$postargs = array(
'posts_per_page' => '-1',
'tag__and' => YOUR_ARRAY,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => YOUR_CUSTOM_POST_TYPE,
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $postargs );
You only need to loop (foreach) through your post-array and output results.

Query all wordpress post titles

I am using Wordpress auto suggests using this snippet of code
and currently it is searching all tags, I want it to search only post titles. Any help is appreciated.
This is sql query calling all the tags which needs to be modified for all posts.
<?php global $wpdb;
$search_tags = $wpdb->get_results("SELECT name from wp_terms WHERE name LIKE '$search%'");
foreach ($search_tags as $mytag)
{
echo $mytag->name. " ";
}
?>
These days i had to do some request in a wordpress theme.
In your case ( getting title can be done easier than getting tags, as in your example link ) the stuff can be done easier (i guess).
Firstly you have to make a php page to get posts. As you maybe know you won't be able to use wp stuff in standalone php files, so your file ( let call it get_posts.php ) will look like
<?php
// Include the file above for being able to use php stuff
// In my case this file was in a folder inside my theme ( my_theme/custom_stuff/get_posts.php ).
// According to this file position you can modify below path to achieve wp-blog-header.php from wp root folder
include( '../../../../wp-blog-header.php' );
// Get all published posts.
$list_posts = get_posts( array( 'numberposts' => -1 ) );
// Get "q" parameter from plugin
$typing = strtolower( $_GET["q"] );
//Save all titles
$list_titles = array();
foreach( $list_posts as $post ) { $list_titles[] = $post->post_title; }
// To see more about this part check search.php from example
foreach( $list_titles as $title ) {
if( strpos( strtolower( $title ), $typing ) ){
echo $title;
}
}
?>
I added some comments trying to help you better.
Now stuff get easy, you only have to call your page through jQuery plugin like
$('#yourInput').autocomplete( "path_to/get_posts.php" );
You can directly use wordpress in-build feature to get all post titles
// The Query
query_posts( 'posts_per_page=-1' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
None of the answers here answer your real question:
How to query JUST post titles
The "raw SQL" way:
Few important things:
escape search for SQL! (also do that for the tags search!) use $GLOBALS['wpdb']->esc_like()
if you only need 1 column, you can use $GLOBALS['wpdb']->get_col()$GLOBALS['wpdb']->get_results() is if you want to fetch more columns in one row
use $GLOBALS['wpdb']->tableBaseName to make your code portable - takes care of the prefixe.g. $GLOBALS['wpdb']->posts
When querying posts you must also think about which post_type and post_status you want to query=> usually the post_status you want ispublish, but post_type may vary based on what you want
WordPress table "posts" contains ALL post types - post, page, custom, but also navigation, contact forms etc. could be there! => I strongly advice to use explicit post_type condition(s) in WHERE ! :)
...$GLOBALS is same as globalizing variabl -today performance difference is little
<?php
// to get ALL published post titles of ALL post types (posts, pages, custom post types, ...
$search_post_titles = $GLOBALS['wpdb']->get_col(
"SELECT post_title from {$GLOBALS['wpdb']->posts}
WHERE (
(post_status = 'publish')
AND
(post_title LIKE '{$GLOBALS['wpdb']->esc_like($search)}%')
)
ORDER BY post_title ASC
"); // I also added ordering by title (ascending)
// to only get post titles of Posts(Blog)
// you would add this to conditions inside the WHERE()
// AND (post_type = 'post')
// for Posts&Pages
// AND ((post_type = 'post') OR (post_type = 'page'))
// test output:
foreach ($search_post_titles as $my_title) {
echo $my_title . " ";
}
?>
The WP_Query way
This is more wordpress but has a little overhead, because although there is a fields param for new WP_Query()/get_posts(), it only has options:
'all' - all fields (also default), 'ids' - just ids, 'id=>parent' - ... if you pass anything else, you still get all, so you still need to add "all" BUT - WP however has filters for altering fields in SELECT.
I tried to make it the same as the raw SQL version, but it depends on how does WP does it's "search" - which I think is %search% for 1 word + some more logic if there are more words. You could leverage the $clauses filter used for fields to also add your custom where INSTEAD of adding the 's' into $args (remember to append to not-lose existing WHEREs $clauses['where' .= "AND ...;). Also post_type => 'any' does not produce always the same results as the raw query in cases like Navigation, Contact forms etc...
Also WP_Query sanitizes the input variables so actually don't escape $args!
<?php
$args = [
'fields' => 'all', // must give all here and filter SELECT(fields) clause later!
'posts_per_page' => -1, // -1 == get all
'post_status' => 'publish',
's' => $search,
// I also added ordering by title (ascending):
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'any', // all "normal" post types
// 'post_type' => 'post', // only "blog" Posts
// 'post_type' => ['post', 'page'], // only blog Posts & Pages
];
$onlyTitlesFilter = function($clauses, $query) {
// "fields" overrides the column list after "SELECT" in query
$clauses['fields'] = "{$GLOBALS['wpdb']->posts}.post_title";
return $clauses; // !FILTER! MUST RETURN!
};
$onlyTitlesFilterPriority = 999;
// add filter only for this query
add_filter('posts_clauses', $onlyTitlesFilter, $onlyTitlesFilterPriority, 2);
// Pro-tip: don't use variable names like $post, $posts - they conflict with WP globals!
$my_posts = (new WP_Query($args))->get_posts();
// !IMPORTANT!
// !remove the filter right after so we don't affect other queries!
remove_filter('posts_clauses', $onlyTitlesFilter, $onlyTitlesFilterPriority);
// test output:
// note that I used "my"_post NOT just $post (that's a global!)
foreach($my_posts as $my_post) {
echo $my_post->post_title . " ";
}
?>
Don't be confused - you will still get the array of WP_Posts - WP will throw some default properties&values into it, but in reality it will only query and fill-in with real values the fields you specify in the filter.
PS: I've tested these - they are working codes (at least on WP 5.4 and PHP7 :) )

Ordering Wordpress posts based on parent category

UPDATE: I have tried using the following code:
<?php if (is_category(events)) {
$posts = query_posts($query_string . '&orderby=event_date&order=desc');
} else {
$posts = query_posts($query_string . '&orderby=title&order=asc');
}
?>
Is there any reason why that wouldnt work? It seems to work fine organising posts in alphabetical order, but still no luck on the date order within 'events'.
--
After searching through various existing questions I can't quite find a solution to what I am trying to do.
Currently all posts on my site are ordered alphabetically, which is fine except for one new category that I have added. For this category I want to order all posts by a value that I enter into a custom field. The field is called 'event_date' - so I want to order the posts by date essentially, but not the date the post was created, the date the user manually enters into this field.
I managed to get it working by using:
<?php if (is_category($events)) { $posts = query_posts($query_string . '&orderby=$event_date&order=asc'); } ?>
However this overrides the aphabetical order for all other pages.
For alphabetical order I am using:
<?php if (is_category()) { $posts = query_posts( $query_string . '&orderby=title&order=asc' ); } ?>
Essentially I want a statement that tells the page to order all posts in aphabetical order, unless the category is 'events', where I want to order them by the custom event date.
As you can probably tell I'm very much front end, not back end so a lot of this is fairly new to me, so any help or advice is appreciated.
To order posts by a custom field value, you need add the custom meta field to the query itself. orderby=meta_value in addition to meta_key=metafieldid will allow ordering in this fashion.
I would use the pre_get_posts hook and modify the query object if get_query_var( "cat" ) (or a similar query var) returns the desired post category.
add_action( "pre_get_posts", "custom_event_post_order" );
function custom_event_post_order( $query )
{
$queried_category = $query -> get_query_var( "cat" );
/*
* If the query in question is the template's main query and
* the category ID matches. You can remove the "is_main_query()"
* check if you need to have every single query overridden on
* a page (e.g. secondary queries, internal queries, etc.).
*/
if ( $query -> is_main_query() && $queried_category == 123 )
{
$query -> set( "meta_key", "event_date" ); // Your custom field ID.
$query -> set( "orderby", "meta_value" ); // Or "meta_value_num".
$query -> set( "order", "ASC" ); // Or "DESC".
}
}
Remember that this approach overrides all queries that are using the category in question. You can build custom WP_Query objects that use their own parameters for constructing loops.
You also should standardize the way you save the custom field data to the database. For dates I prefer using UNIX-timestamp formatted times that are easy to move around and manipulate. This way no accidents happen when querying and some data is formatted in another way that the rest is.
Disclaimer: I did not have the time to test the above code in action, but the general idea should work properly.
EDIT: of course the above code should be inserted to functions.php or a similar generic functions file.
What about:
<?php $posts = query_posts($query_string . (is_category($events)?'&orderby='.$event_date:'&orderby=title') . '&order=asc'); ?>
<?php
$recent = new WP_Query(“cat=ID&showposts=x”);
while($recent->have_posts()) : $recent->the_post();
?>
Hopefully I understood your question, use the WP_Query and within the orderby add a space between your order by columns:
$args = array(
'posts_per_page' => 100,
'orderby' => 'title',
'order' => 'ASC',
);
if(is_category($events)){
$args['orderby'] .= " meta_value_num";
$args['meta_key'] = 'event_date';
}
$posts = (array) new WP_Query( $args );

Sort meta_value from wp_query but remove special characters before the sort

I have a plugin that will sorts posts on the search page and taxonomy page. The loop has already taken care of narrowing down the results, so what i need is to be able to sort the post my a custom meta value. In this case price. I want to be able to reach into the existing query, replace any special characters, then sort based on the clean meta value.
I used this select, but the problem is it querys all posts on the database instead of the reults found in wp_query and this can slow down the sort tremendously
function join_it( $join ) {
global $wpdb;
$sym = get_option('sort_by_ignore');
$join .= "LEFT JOIN (SELECT $wpdb->prefix" . "postmeta.post_id AS ID, CAST(REPLACE(REPLACE(REPLACE($wpdb->prefix" . "postmeta.meta_value, ',', ''), '" . $sym . "',''),' ','') AS SIGNED) AS price FROM $wpdb->prefix" . "postmeta WHERE meta_key = 'my_price'" . ") P ON (P.ID = wp_posts.ID)";
return $join;
}
add_filter('posts_join', 'join_it' );
$args = array_merge( $wp_query->query, array('caller_get_posts' => 1,'paged' => $paged ));
query_posts($args);
If you're only using it for sorting, you may be better off using the posts_orderby filter, rather than posts_join.
If you're finding the filter's affecting other queries, you could use remove_filter once it's run where you need it.
My answer here does something vaguely similar (without cleaning the values with the replace function - you'd have to leave that in, I think).

Categories